How do I Ping.Pong between two quaternions?

Hey all,

I’ve tried to digest the Unity docs on the relevant parts, but I’m really struggling to understand what’s going on.

What i’m trying to do is rotate between two set angles, and back again and basically keep doing this. (Ping.pong).

private var from : Quaternion;
private var to : Quaternion;

function Start(){
from = transform.rotation * Quaternion.Euler(transform.up * 90);
to = transform.rotation * Quaternion.Euler(transform.up * 270);
}

function Update(){

   	//transform.rotation = Quaternion.Slerp (from, to, Time.deltaTime * speed);
  	//transform.rotation = Quaternion.Euler(0.0, Mathf.Repeat(from, to), 0.0);

}

I have commented out the actual attempts I’ve had, as they’ve not been successful.

Any ideas?

Thanks, Bobble.

About Fattie’s comment: You should always use quaternions if you are dealing with rotations. Manipulating the euler angles directly does not interpolate well. Unity provides a lot of easy to use functions to built working quaternions without even having a clue how they work.

At topic:

Just use Math.PingPong on the parameter of Quaternion.Slerp:

var t : float = 0.0;

function Update{
    t = Mathf.PingPong(Time.time * speed, 1.0);
    transform.rotation = Quaternion.Slerp (from, to, t);
}