- Home /
A constant speed rotation finishing with a correct angle.
I am using the following code :
void Update()
{
var oldRotation = transform.rotation;
Quaternion newRotation = Quaternion.Euler(0, 0, 90);
transform.rotation = Quaternion.Lerp(oldRotation, newRotation, Time.deltaTime );
}
A ~= 90 degree rotation around Z is performed, but the rotation speed is more and more slow and at the end my rotation is of almost 90 but not exactly.
I need to perform a rotation of exactly 90 with a constant speed. How can i fix my code to perform such a thing?
Answer by Owen-Reynolds · May 03, 2011 at 11:10 PM
I find it's best to keep code simple, so you can understand and change it. Step 1 is to make something go from 0 to 90. Step 2 is to rotate that much:
var zTilt : float = 0; var degsPerSec : float = 45; // change to whatever speed you want
void Update() { // make zTilt slowly get bigger: zTilt = zTilt + Time.deltaTime * degsPerSec; if(zTilt > 90) zTilt = 90;
transform.rotation = Quaternion.Euler(0, 0, zTilt); }
That particular use of Lerp is for exactly what you wrote -- more fast than slow down and never quite get there. It's good for going to a moving target.
Your answer
Follow this Question
Related Questions
Rotation Speed Hinge Joint 2 Answers
Images Rotation 1 Answer