# Rotate something

I’m trying to rotate something along its local z axis a set amount of degrees.

I found this snippet of code somewhere here on the forums and it works great except if the object is currently rotating, in which case it rotates on more than one axis.

``````function Spin (degreesToSpin : float, spinTime : float) {

var startRotation = transform.rotation;
transform.Rotate(Vector3.forward * degreesToSpin);
var endRotation = transform.rotation;
var t = 0.0;
while (t < 1.0) {
t += Time.deltaTime * (1.0/spinTime);
transform.rotation = Quaternion.Slerp(startRotation, endRotation, t);
yield;
}
}
``````

Any ideas on how to solve this problem would be splendid.

``````transform.localEulerAngles = Vector3(0, 0, 45);
``````

That causes the object to rotate in one frame, so it jumps from one position to the next. I figured that much.

What I was trying to accomplish was getting it to smoothly transition from one rotation to another.

It’s not really clear from your question what you are trying to achieve? - you perhaps need to be more specific about why the object would be currently rotating?

Always best to read you posts though and think to yourself “will anyone be reasonably able to help me given the information I’ve provided” before you hit submit!

This code will rotate the object that has the script from one rotation to another (only on the z axis).

``````var rotationA = Quaternion.identity;
var rotationB = Quaternion.identity;
var speed = 0.1;

function Start (){

rotationA.eulerAngles = Vector3(0, 0, 0);
rotationB.eulerAngles = Vector3(0, 0, 45);

}

function Update (){

transform.rotation = Quaternion.Slerp (rotationA, rotationB, Time.time * speed);

}
``````

… now, perhaps (and this is only a guess) before you do the rotation, you need to set:

``````rotationA = transform.rotation;
// ie. make this rotation the current rotation

rotationB = rotationA + Vector3(0, 0, 45));
// ie. make this rotation the current rotation PLUS 45degrees on the Z axis.
``````

OR

you could apply the ablove to the main parent object, whilst still letting a child of this object freely rotate ‘within’ it?

I don’t know, because I don’t know what you want…