# Quaternion Hell

Can somebody put me out of my misery. I’ve checked a load of threads on the subject but i still don’t really get it. OK I don’t want gimbal lock any more than the next man but i do want to rotate game objects in a predictable fashion. Let me try and to get to the quick of it. Say I want to use an expression such as:

``````transform.rotation = Quaternion.Slerp (oldRotation, newRotation, Time.time * t);
``````

I know oldRotation because it’s the current value of transform.rotation (i.e. a quaternion) but all I know about newRotation is that it’s (x,y,z) degrees. How do i calculate newRotation Anyone speak quaternion?

the argument for this function is 3 seperate variables rather than a single Vector 3 so if i need to define newRotation as an exposed variable i do this right?

``````var newPostion.x : float;
var newPostion.y : float;
var newPostion.z : float;

newRotation = Quaternion.Euler(newRotation.x, newRotation.y, newRotation.z);
``````

One of the things that’s confusing for beginners is the relationship between the transform as seen in the inspector and the transform as used in scripting. You expect it be an object with 3 (Vector 3) variables.
BTW Am i right in assuming scale is just not on the menu as far as scripting goes?

Transform.localScale for all your scaling needs…

No, read down the page a bit more:

Although you can use 3 separate variables too if you want.

–Eric

One final question on this thread. This little co-routine works but produces quite idiosyncratic behavior. e.g random amounts of ‘faffing’ around before starting the movement, somtimes nothing for 1/2 the time then a sudden lurch. It’s fine until the distances get above 10.0 or so and It’s much worse if i substitue Slerp for Lerp.

``````function Move ()
{
newRotation = Quaternion.Euler(dockRotation);
for (t=0.0; t<=1.0; t+=Time.deltaTime * dockingSpeed)
{
transform.position = Vector3.Lerp(transform.position, dockPosition, t);
transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, t);
yield;
}
transform.position = dockPosition; 	//correct final position
transform.rotation = newRotation;
}
``````

[/code]

Don’t you want something like (also rewrote the loop slightly to avoid having to set the position/rotation again outside the loop):

``````function Move ()
{
startPosition = transform.position;
startRotation = transform.rotation;
newRotation = Quaternion.Euler(dockRotation);
t = 0.0;
while (t < 1.0) {
t+= Time.deltaTime * dockingSpeed;
transform.position = Vector3.Lerp(startPosition, dockPosition, t);
transform.rotation = Quaternion.Lerp(startRotation, newRotation, t);
yield;
}
}
``````

–Eric

Don’t you want something like…

yes eric i reckon i do - thanks

i can see why it got worse over larger distances now.