- Home /
Object Rotation Relative to Camera
Hey guys, fairly simple question:
I've got a menu system that consists of 2D planes in world space with a script that makes them always face the camera:
function Update () {
// Face the camera directly.
transform.LookAt(Camera.main.transform.position);
// Rotate so the visible side faces the camera.
transform.Rotate(90, 0, 0);
}
The planes are on a culling mask with a secondary camera so they render on top of everything else... fairly standard, looks like this at best (the icons/text are located around the turret, in world-space):
Trouble is, when you view an object from a tilt above or below the horizontal, the rotation of the objects does not match the rotation of the camera view, ending up in a derpy situation such as this:
I need a way to keep the icons/text horizontal relative to the screen. Any idea how I could pull this off?
Thank you!
Thanks as always, Fattie! $$anonymous$$ike's solution worked but I'm investigating worldUp for future use.
I've been trying to figure out how to make a more natural placard orientation, but haven't managed to make it happen without parenting the plane to a something. Thanks for the tip though--I'll keep messin with it.
$$anonymous$$ike has aced in, I'm too drunk - the "up direction" thing is a bit different, yes hope it helps one day !!
Answer by whydoidoit · Sep 19, 2012 at 09:21 AM
This might do it:
transform.rotation = Quaternion.LookRotation(-Camera.main.transform.forward, Camera.main.transform.up);
That did the trick. Thanks $$anonymous$$ike!
I've trawled the internet look for the answer to this - and finally found it - thank you ever so much! :D
Looked everywhere for this line of code. Adjusted it to suit my 3rd-person perspective needs.
transform.rotation = Quaternion.LookRotation(new Vector3(Camera.main.transform.forward.x, 0, Camera.main.transform.forward.z));
Thank you so much. Still working in 2020.
Your answer
Follow this Question
Related Questions
Replicate rotation of Camera 1 Answer
Rotate object ON THE CAMERA UP AXIS 2 Answers
Make object appear within camera view 0 Answers
Help, please with rotation input 0 Answers
Wonky Camera Behaviours 0 Answers