Search Unity

Issue with WSAD movement.

Discussion in 'Editor & General Support' started by tempusrex, Aug 18, 2019.

  1. tempusrex

    tempusrex

    Joined:
    Dec 12, 2017
    Posts:
    3
    Hey guys, so i'm messing around with some simple 3D controls and I have run into an issue whereby the character will rotate at the last second after movement has stopped. This only tend to happen when is use a combination of WSAD controls. I was wondering whether this has something to do with the very last input taken at the end of a frame (so if I took my finger off W just before D then the character will snap right just at the end of movement instead of staying diagonal). I've tried editing the gravity setting in the input setting to no avail. This one has stumped me a bit with my current understanding of Vectors and was wondering if you guys have any solutions, Thanks.

    Code (CSharp):
    1. public class PlayerMovement : MonoBehaviour
    2. {
    3.     public float speed = 10.0f;
    4.     void Start()
    5.     {
    6.     }
    7.     void Update()
    8.     {
    9.         float moveVertical = Input.GetAxis("Vertical");
    10.         float moveHorizontal = Input.GetAxis("Horizontal");
    11.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    12.         transform.Translate(movement * speed * Time.deltaTime, Space.World);
    13.         if(movement != Vector3.zero)
    14.         {
    15.             Quaternion LookRotation = Quaternion.LookRotation(movement);
    16.             transform.rotation = LookRotation;
    17.         }
    18.      
    19.     }
    20. }