# Rotate based on another objects axis

I am having trouble understanding angleAxis. What I want to do is create a rotation that is 30 degrees rotated around the cameras axis.

``````transform.rotation *= Quaternion.AngleAxis( 30, cam.right);
``````

But the result if this is not what I expected, what am I doing wrong?

It depends on what you expected But, a couple of possibilities…

Do you want the camera to rotate left/right? If so, the axis you want is Vector3.up - think of the axis vector as a rod inside a tube that rotates around it.

Did you want the camera to rotate a certain amount from a fixed starting position? If so, you shouldn’t use the *= operator because this will rotate the object starting from wherever it is currently.

Perhaps you could give a bit more detail about what you are doing if neither of these things helps.

Yeah i’m sorry I am having a hard time explaining myself.

I am trying to make my third person character aim up and down ( Like in uncharted, resident evil etc). I have the horizontal rotation working fine, but I am having trouble making him rotate vertically ( I.e. make him look at the ground or the sky). So what I am trying to do is rotate his spine bones forwards based on where the camera is looking.

So I want to rotate the spine around cam.right axis, 30 degrees.

I think you need Quaternion.eulerAngles to get the rotation around the camera’s X axis. You can then apply this angle to the object’s rotation:-

``````var angle: float = cam.transform.rotation.eulerAngles.x;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.right);
``````
``````var temp = cam.TransformDirection (Vector3.right);
spine3.Rotate(temp * (Time.deltaTime * 10), Space.World);
``````

Okay as a test I did this. This makes the spine rotate in the direction I want it to go, now what I want to do is make it rotate X degrees in that direction, instead of rotating indefinitely.

So I need to replace the second line with angle axis

I tried

``````spine3.rotation = Quaternion.AngleAxis( 45, temp);
``````

But this does not seem to work.

I think you actually do want the *= operator in this case:-

``````spine3.rotation *= Quaternion.AngleAxis( 45, temp);
``````

Argg. Im still stuck on this. Now the spine rotates in the wrong direction.

Is angleAxis what I am supposed to be using for this?

If you reverse the direction of the axis (use the minus operator in front of the vector) then the rotation will be reversed.

No I mean its rotating on a different axis than it was when I was using transform.rotate

Sorry, I’ve overlooked something here - the equivalent of transform.Rotate is actually to set transform.localRotation, not transform.rotation.

hazzah, I got this all figured out now, thanks for your help.