- Home /
How to make a plane face a static but rotating camera?
Hello,
SCENARIO:
I have a plane with has UI Elements on it. It is a child to an empty GameObject (say targetGO). Now I wish to rotate targetGO towards the Main camera and make sure it faces it always. The camera is completely static but rotates around it's position.
PROBLEM:\
It works fine, but for some reason the plane is not perfect. Hence I believe that there is something fundamentally wrong in the logic. I use this piece of code. This class is a component of targetGO.
CODE :
private void lookAtCamera()
{
transform.LookAt((this.transform.position + cameraMain.rotation * Vector3.up),
(cameraMain.rotation * Vector3.up));
}
// EOF
REQUEST :
Is there a problem with the logic, if not what is the best route to approach it. I am sure this has been done many times but I wish to know the correct flawless logic.
Thank you and highly appreciate your time.
Karsnen
Answer by robertbu · Nov 17, 2014 at 09:08 PM
I'm not sure of your exact goal here, but the easiest way to make sure a 'plane' is facing the camera is to use a Unity's built-in Quad (not a Plane). Then you can just assign the rotation:
transform.rotation = cameraMain.rotation;
I'm assuming 'cameraMain' is the transform of the main camera.
I'm sorry, but no. All this would do is assign the same rotation to the gameObject as the camera. That will NOT make it face the camera. It may actually make it face AWAY from the camera...
@FairGamesProductions - you are wrong. Unity's Quad has the visible triangles on the backside. So if the rotation of the camera and the rotation of the Quad matches, and the Quad is within in the Camera's view Frustum, then the Quad will be visible. In addition, since the rotation matches, the orientation will always match. The rotation I believe the OP is trying for can be done with a Plane, but the code is a lot uglier.
@robertbu No... YOU are wrong. If you give the built-in plane, the exact rotation of the camera, it will NOT be facing the camera AND there is a good chance that the backward facing polygon will be facing the camera, so the polygon will, if effect, be invisible without the right shader...
No, read my answer. I recommend a built-in Quad. In addition, I just dropped in a built-in plane into a project, added a material and this script using your line of code:
#pragma strict
function Update () {
transform.LookAt(Camera.main.transform.position);
}
As expected, it does not work. Nor does it work for a Quad. But the line of code I supplied works just fine with a Quad.
I agreed with you (see lower) about the visible polygons on the quad. But simply taking the rotation of the camera and applying it to the quad will fail eventually. Don't forget, he said that the camera is stationary, BUT it can rotate. LookAt is the only surefire way of doing it.
Answer by FairGamesProductions · Nov 17, 2014 at 09:12 PM
If I understand your question correctly, you only need this to have the gameObject look at the camera:
transform.LookAt(cameraMain.transform.position);
I'm not sure why you have all the rest there... Also, this should be under the Update function, otherwise it will only happen once when the lookAtCamera function is called. You want this to happen all the time in order to keep the gameObject pointed at the camera at all times.
EDIT: As robert has pointed out, the built-in quads have their visible polygons on the negative Z axis (which is really weird, IMHO). So an extra line of code is needed:
function Update ()
{
gameObject.transform.LookAt(Target.transform.position);
gameObject.transform.rotation.eulerAngles.y += 180;
}
This will correct the Y rotation so the visible polygons are facing the camera.
This will not work with a built-in plane, nor will it work with a Quad. In addition, it does nothing to assure the orientation. That is if the camera is rotated arbitrarily, the object will face the camera, but the orientation may be anything.
That is just not true...
LookAt will point the positive Z axis of the object at the point you are targeting... It has NOTHING to do with the rotation of the camera... And FYI, I use this with the built-in quads in my game and it works PERFECTLY.
But a built-in plane, the visible triangles are facing positive 'y'. A Quad has the visible triangles on the back side. Neither of these objects has the visible side on the positive 'z' size. And the positive 'z' side is the side that LookAt() will face towards it supplied Vector3. Test it.
O$$anonymous$$. You're right. I got a little confused. I was thinking of C4D for some reason. But it's a simple fix:
function Update ()
{
gameObject.transform.LookAt(Target.transform.position);
gameObject.transform.rotation.eulerAngles.y += 180;
}
This will now work for built-in quads.
Answer by philwinkel · Nov 17, 2014 at 09:15 PM
I think what you're trying to accomplish is a "Camera-facing billboard", here is some information: http://wiki.unity3d.com/index.php?title=CameraFacingBillboard
As FairGamesProductions suggested, you can use transform.LookAt, or there are some other samples on that wiki link depending on your requirements.
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
[Solved] Help on a rotation issue for a Top-Down view? 1 Answer
rotate rigid body based on transform.velocity? 2 Answers
i want to get my to cube to rotate 90 degrees on the x-axis as it jumps 0 Answers
Getting the rotation of an object does not return correct rotation 1 Answer