- Home /
Rotate game object and then return to its original rotation
I am trying to make an AirCraft that moves, and when you turn left or right, it leans in that direction smoothly, and when you release the turn key, it returns to its original rotation (smoothly again, but a bit faster).
So far I have this, and it only rotates the plane a little, and doesn't return to its original rotation. Whats wrong?
if (Input.GetKey(KeyCode.LeftArrow)) {
transform.Translate (-1f, 0f, 0f);
if (leftLeaning > 0.0f) {
transform.Rotate(0f, 0f, 1.5f);
leftLeaning--;
}
}
if (!Input.GetKey(KeyCode.LeftArrow)) {
if (leftLeaning <= 1.0f) {
transform.Rotate(0f, 0f, 0.2f);
leftLeaning++;
}
}
Also I don't know how much in float numbers is 90 degrees and 0 degrees.
Answer by dudester · Jun 30, 2015 at 12:39 PM
do this instead cause you cant use rotate to return to a rotation , rather use quaternion.slerp
if (Input.GetKey(KeyCode.LeftArrow)) { transform.Translate (-1f, 0f, 0f); if (leftLeaning > 0.0f) { transform.Rotate(0f, 0f, 1.5f); leftLeaning--; } } if (!Input.GetKey(KeyCode.LeftArrow)) { if (leftLeaning
you might need to use localrotation instead of rotation in the slerp function but some testing will determine if its needed