![]() Next, we need to get the desired destination rotation. We want to make the character rotate towards the movement direction at a specific rate, so we've added a public variable for the rotation speed. ![]() Quaternion toRotation = Quaternion.LookRotation(movementDirection, Vector3.up) transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, rotationSpeed * ltaTime) įirst of all, we've deleted the line where we set the forward direction. Transform.Translate(movementDirection * speed * Time. Vector3 movementDirection = new Vector3 (horizontalInput, 0, verticalInput) GetAxis( "Horizontal" ) įloat verticalInput = Input. Public float rotationSpeed void Update ()įloat horizontalInput = Input. We'll set the speed to five in the Inspector panel. Let's save the script and switch back to Unity. We're also multiplying by ltaTime to ensure it moves at the same speed regardless of our framerate.įinally, we're also specifying that we are moving the character relative to the World. We're then multiplying the direction by this speed. We want to be able to control how fast the character moves, so we've created a public variable for the speed. ![]() Next, we're changing the position of the character based on the movement direction by accessing the object's Transform and using the Translate method to move it in the direction we want. We're then normalising the direction vector to ensure it has a magnitude of one. ![]() We're setting the direction on the X axis to our horizontal input, the direction on the Y axis to zero as we don't want the character to move up and down, and the direction on the Z axis to our vertical input. We're then using these input values to create a three dimensional vector for the direction we want the character to move. We're also doing the same for the vertical axis. In our Update method, we're getting the player input on the horizontal axis and assigning it to a variable. Transform.Translate(movementDirection * speed * ltaTime, Space.World) Vector3 movementDirection = new Vector3(horizontalInput, 0, verticalInput) ![]() Public class PlayerMovement : MonoBehaviourįloat horizontalInput = Input.GetAxis( "Horizontal") įloat verticalInput = Input.GetAxis( "Vertical") ![]()
0 Comments
Leave a Reply. |