- Home /
Problem - Getting Object to Face Camera Plane
Hello,
I'm using this code:
var rotation = Quaternion.LookRotation(-Camera.main.transform.forward);
transform.rotation.y = rotation.y;
To get my GUI objects to always face the plane direction of my camera, works fine until I reach a certain angle, which is about 170, lower than this and the GUI doesn't move, unless I keep pivoting around, and then it will 'snap' facing the camera again at an angle of 289.
Any reason why it is doing this?
Answer by whydoidoit · Oct 07, 2013 at 03:50 PM
Your problem is that you are trying to set the Y of a Quaternion - you shouldn't fiddle with the elements of a quaternion directly - you probably mean to use the eulerAngles. The y of a Quaternion is not the rotation about the y axis.
So how do I use 'LookRotation' without using Quaternion?
Okay, all I did was this:
transform.forward.x = Camera.main.transform.forward.x;
transform.forward.z = Camera.main.transform.forward.z;
Answer by raimon.massanet · Oct 07, 2013 at 02:43 PM
Why not use tranform.LookAt
instead?
That doesn't work. See http://unitygems.com/quaternions
@whydoidoit After reading the link you posted, I believe what my problem is, is: Gimbal lock effect. When one rotation aligns with another, they get locked together and one dimension is lost.
I believe transform.LookAt
should work for UI elements facing a camera... @whydoit Can you point me to the section that states otherwise in the link you posted?
The problem with using LookAt is this (the red boxes are GUI elements within my scene, this is looking top down at the scene), plus, I only want the elements to rotate on the Y axis:
It's the bit about how to get objects to point at the camera - the camera is a point in space so LookAt only works at significant distance. The items should point at the inverse of the the camera's forward, not at it's position.
See the bit where it says this:
When you get close to the enemies you see that there is another problem, the Zzzzzz aren't pointing outwards from the screen. A lot of people are confused by this (I know I was) - we have a script that says LookAt the camera - why isn't it looking at it?
Your answer
Follow this Question
Related Questions
Rotating a plane so that its top is parallel to the camera viewport 1 Answer
Camera Loop Rotation 0 Answers
Attach image to button 1 Answer
Bind horizontal scrollbar to camera control 1 Answer
3D effect with new UI system 1 Answer