- Home /
Rotate an object relative to Camera
I have a cube that I am rotating with the keyboard, like so (inside update() )
// Get delta from inout
Quaternion rotX = Quaternion.AngleAxis (Input.GetAxis ("Horizontal") * rotationSpeed, Vector3.right) // X rotation
// Apply rotation
this.transform.rotation *= rotX;
// Do the same for Y…
When I press left, the object rotates to the left as expected, the same goes for right. However when I move my camera around the back, pressing left makes it rotate right, and pressing right makes it rotate left. I figured out that if I work out the rotation between the camera and the object, I should be able to subtract this from my overall rotation and get the difference (works on paper) but I can't seem to figure out how to implement this in code. The nearest I can get is to make my object always face the camera :-(
I would like the left key to rotate "left" no matter the camera position (and of course to work with the Y and Z axis, as I intend to replace the keyboard with a LapMotion device).
Any suggestions?
Thanks Subhajit, that got me to my final solution which was to use Camera.main.transform.right rather than Vector.right - thanks again!
Answer by Subhajit-Nath · Nov 28, 2013 at 05:30 PM
If you need opposite rotaton, try Vector3.left instead of Vector3.right.
Answer by andreasng_pd · Nov 24, 2018 at 12:09 PM
Hey this is not a thorough answer. I think what is needed is a way to make objectA rotate around objectB(camera) up axis, for example. I'm currently searching the same thing, and many none-answers pop up.
So if the camera is one its head, and you press right, the object will rotate around the cameras local Y axis, and therefore, relative the the world, rotate the "wrong" way.
etc
Your answer
![](https://koobas.hobune.stream/wayback/20220613123020im_/https://answers.unity.com/themes/thub/images/avi.jpg)