Language

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.

@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...

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.

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:

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.

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.