x


Rotate from angle to angle

Currently I am trying to code a 'ping pong' effect. I would like the object to start at angle 0, than rotate to angle 90, than rotate back to 0, etc...

What would be the easiest way to achieve this? Can I use Lerp? Any tips on an optimal setup would be great.

Thanks!

more ▼

asked Jul 07 '11 at 05:59 PM

inewland gravatar image

inewland
16 1 2 3

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

2 answers: sort voted first

Strangely enough, Mathf.PingPong is really good for 'ping pong' effects. ;)

var rotateSpeed = 50.0;

function Update () {
    transform.rotation = Quaternion.Euler(0.0, Mathf.PingPong(Time.time * rotateSpeed, 90.0), 0.0);
}
more ▼

answered Jul 07 '11 at 06:49 PM

Eric5h5 gravatar image

Eric5h5
81.5k 42 133 529

Oh my, how did I miss the Mathf.PingPong?

Nice!

Jul 07 '11 at 07:22 PM inewland
(comments are locked)
10|3000 characters needed characters left

You could always do:

function Update(){
if(transform.eulerAngles,y = 0) {
transform.eulerAngles.y = 90;

} if(transform.eulerAngles.y = 90){
transform.eulerAngles.y = 0;
}
}

You could also use quaternion.fromtorotation to get this effect. Finally instead of transform.eulerAngles.y = 90 or 0, you could use mathf. lerp from 90 to 0 or 0 to 90.

more ▼

answered Jul 07 '11 at 06:23 PM

Macdude2 gravatar image

Macdude2
277 16 20 23

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

x747
x300
x3

asked: Jul 07 '11 at 05:59 PM

Seen: 1780 times

Last Updated: Jul 07 '11 at 07:23 PM