x


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?

more ▼

asked May 03 '11 at 10:24 PM

Sylario gravatar image

Sylario
25 7 7 12

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered May 03 '11 at 11:10 PM

Owen Reynolds gravatar image

Owen Reynolds
11.2k 1 7 45

(comments are locked)
10|3000 characters needed characters left

See the Rotation function here.

more ▼

answered May 03 '11 at 11:44 PM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2155
x355

asked: May 03 '11 at 10:24 PM

Seen: 1136 times

Last Updated: May 03 '11 at 10:24 PM