Search Unity

Auto Correcting Car Direction

Discussion in 'Scripting' started by Foolsbry, Nov 27, 2014.

  1. Foolsbry

    Foolsbry

    Joined:
    Sep 16, 2014
    Posts:
    2
    Hey there, I'm making an endless runner style game where you control a car. The car itself never actually moves, only along the x-axis to move left and right. There are jumps and obstacles etc in the game which the car can collide with.

    That's where my trouble starts. Because the car is supposed to look like it's always moving forward, I tried to write a script that checks if the car's current rotation is what it should be for it to face forward, and if it's not, then use the Quaternion.Lerp function to smoothly correct itself.

    Code (CSharp):
    1. public class SimplePlayerControl : MonoBehaviour
    2. {
    3.     public float PlayerSpeed;
    4.     public float turnAngle;
    5.     public float RotateSpeed;
    6.     private float PlayerInput;
    7.     private float PlayerMove;
    8.     public Transform forward;
    9.  
    10.  
    11.     void Update()
    12.  
    13.     {
    14.             PlayerInput = Input.GetAxis ("Horizontal");
    15.             PlayerMove = PlayerInput * PlayerSpeed;
    16.         Quaternion reset = new Quaternion (0, 0, 0, 0);
    17.     }
    18.  
    19.     void FixedUpdate ()
    20.  
    21.     {
    22.         rigidbody.velocity = new Vector3(PlayerMove, -5.8f, 0.0f);
    23.         CheckRotation ();
    24.  
    25.  
    26.         //rigidbody.rotation = Quaternion.Euler (rigidbody.velocity.y, rigidbody.velocity.x * turnAngle, rigidbody.velocity.z);
    27.  
    28.     }
    29.  
    30.     void CheckRotation()
    31.     {
    32.         if (rigidbody.rotation != forward.rotation);
    33.         {
    34.             rigidbody.rotation = Quaternion.Lerp(rigidbody.rotation, forward.rotation, Time.deltaTime * RotateSpeed);
    35.         }
    36.     }
    37. }
    38.  
    With this code it *almost* works, however when the rotation is too out of whack the car sometimes hovers in the air, it always shakes wildly and it sometimes floats off slowly into space even though on the rigidbody I've locked it's position on the z-axis.

    The transform it references is an empty gameObject in the world with it's rotation values all set to 0.

    The second part I need help with is making the car turn slightly when it moves left or right. The nulled out line in FixedUpdate is what I had and it was working for the most part but it had similar problems. The car would shake wildly and gravity had a much less severe effect on it. It is just code from the Space Shooter tutorial from the Unity guys, but I'm sure there's got to be a better way of doing it.

    Any help would be greatly appreciated! Thanks!

    EDIT - I just had a thought, I tried to use transform.LookAt to make the player look in the desired direction (forward). It didn't work, but it might be along the lines of what I might have to do. Is there a way to Lerp the transform.LookAt so it is smooth? Also when the player went over a jump, it appears as gravity was completely disabled for some reason.
     
    Last edited: Dec 3, 2014
  2. Foolsbry

    Foolsbry

    Joined:
    Sep 16, 2014
    Posts:
    2
    Edited the main post. I thought it would bump it without having to double post but it didn't.