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 :

http://pastebin.com/TWLurbga

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

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.

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.

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.