car controler


I made the unity scripting tutorial and I want to change one element, if the car is stoped, you are able to rotate it. I want to allow that if the car is moving only.

Any idea on how to do it?

Since you do not have a rigidbody, if you do not mind brute force you can do the following:

  1. Add a variable that is a Vector3 to hold the prior position of the car
  2. Check the distance between the current position of the car and the prior position
  3. If the distance is greater than some value, apply the turn


// The Update function is called once every frame

var priorPosition : Vector3;

function Start () {
    priorPosition = transform.position;

function Update () {

    distance = Vector3.Distance(priorPosition, transform.position);
    Debug.Log("v:" + distance);
    priorPosition = transform.position;
    // Apply motion along the z axis of the car
    transform.Translate (0, 0, Input.GetAxis ("Vertical"));

    if (distance > 0.1) {
        // Apply rotation around the y axis of the car
       transform.Rotate (0, Input.GetAxis ("Horizontal"), 0);

Thank you ifrog,

I follow the tutorial and there they aply a rigid body, can I apply the script without disable the rigid body?

Thank you in advance

You should be able to. If you are using the rigid body to move the car by adding forces, rigidbody.AddRelativeForce() and rigidbody.AddRelativeTorque(), then you can query the rigid body for its velocity. If you do that the check the square magnitude of teh velocity to see if it is less than a given value.

Many thanks!!!

I will try it