Search Unity

Issue with movement controls.

Discussion in 'Scripting' started by tempusrex, Aug 16, 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). 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.  
    5.     void Start()
    6.     {
    7.  
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.         float moveVertical = Input.GetAxis("Vertical");
    13.         float moveHorizontal = Input.GetAxis("Horizontal");
    14.  
    15.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    16.  
    17.         transform.Translate(movement * speed * Time.deltaTime, Space.World);
    18.  
    19.         if(movement != Vector3.zero)
    20.         {
    21.             Quaternion LookRotation = Quaternion.LookRotation(movement);
    22.             transform.rotation = LookRotation;
    23.         }
    24.        
    25.     }
    26. }
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Look into the input system(in the toolbar), when you release a key it doesn't go to 0 immideatly, you can change it with the 'gravity' setting on the axis.
     
    gononono64 likes this.
  3. tempusrex

    tempusrex

    Joined:
    Dec 12, 2017
    Posts:
    3
    This hasn't seemed to have fixed the issue. It seems like my player character will not stop in a diagonal position and will always snap to a angle of 90.