x


What's the correct way of smoothly rotating from an initial to a target position over several frames?

Say for example I want to rotate my object from an inital rotation to a final one over a several frames.

Is the below code snippet the right way to do this? If not, how should I go about this? (assume I've set myStartTime to Time.realtimeSinceStartup when I started doing the rotation, and that initialRotation and finalRotation have been defined somewhere else as well)

void Update() { float elapsedTime = Time.realtimeSinceStartup - myStartTime;

float timeFraction = elapsedTime / 10.f;// take 10s to get from initialRotation to finalRotation

myGameObject.transform.rotation = Quaternion.Slerp(initialRotation, finalRotation, timeFraction);

}

more ▼

asked Dec 14 '10 at 11:12 PM

bm212 gravatar image

bm212
63 4 4 11

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

1 answer: sort voted first

Update is for things that happen every single frame. That's not appropriate here since you don't want the rotation to continue forever, only up to a point. Use a coroutine, like this.

more ▼

answered Dec 14 '10 at 11:25 PM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

(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:

x2171
x442
x117

asked: Dec 14 '10 at 11:12 PM

Seen: 558 times

Last Updated: Dec 14 '10 at 11:12 PM