- Home /
how to rotate a car smoothly?
Hello guys, i am working on Car AI and i want my cars to change the lanes aswell. everything is going right except the rotation of car, it is not smooth.i also have to set the rotation to 0 when the car has changed its lane.
i am doing like this.
transform.Rotate(0, rotationValue, 0); // rotationValue = -5 or 5
and when it has changed the lane i set the rotationValue to 0. Any help or hint will be appreciative
thanks
Answer by aldonaletto · Oct 06, 2011 at 02:05 PM
Rotate should be used only for continuous rotations, because it can accumulate errors and let your model tilted to some weird angle after some time. For tilting and other small angle changes, it's always better to set transform.localEulerAngles.
The function below use this property to turn the car like you said: turns smoothly to the angle during half the time, then turns back to zero in the other half. Additionally, it "banks" to the side automatically, like real cars do when changing lanes - if you don't need this, replace bank by zero when setting the localEulerAngles.
You may place this code in the car script or in any other object, because you must pass the car transform when calling the function. Since it is a coroutine, you just call it when starting changing lanes, and the routine does the rest automatically.
I included a test Update function that I used to fine tune the routine - attach this script to the car and use the keys A or D to change to the right or left lane.
private var turn: float = 0; private var changing = false;
function ChangeLane(car:Transform, angle: float, time: float){ var t: float; var bank: float; if (changing) return; changing = true; for (t = 0; t < 1;){ t += 2*Time.deltaTime/time; turn = Mathf.Lerp(turn, angle, t); bank = 0.5 turn; car.localEulerAngles = Vector3(0, turn, bank); yield; } for (t = 0; t < 1;){ t += 2*Time.deltaTime/time; turn = Mathf.Lerp(turn, 0, t); bank = 0.5 turn; car.localEulerAngles = Vector3(0, turn, bank); yield; } changing = false; }
// This is just for testing purposes - you must call ChangeLane in your script // when changing lanes. Attach this script to the car to test it.
function Update(){ if (Input.GetKeyDown("a")){ ChangeLane(transform, 5, 1); } if (Input.GetKeyDown("d")){ ChangeLane(transform, -5, 1); } }
Your solution is good but i have also applied rigidbody on my car and it does not move my car smoothly and giving me some weird effects aswell.
You can use a trick: have a main object with the rigidbody, and child your car model to it. When changing lanes, pass the car model transform to ChangeLane - only the model will turn and bank, and the main object with the rigidbody will not be affected.
Answer by malik.tariq49 · Jan 20, 2015 at 11:44 AM
Try this code if (Input.GetKey (KeyCode.LeftArrow)) {
transform.Rotate (new Vector3 (0f, -1f, 0f)); } if you go for right arrow then value of y should be plus just like that (0f,1f,0f )
Your answer
![](https://koobas.hobune.stream/wayback/20220613000046im_/https://answers.unity.com/themes/thub/images/avi.jpg)