Make an instantiated plane face the camera at all times

I am creating a 2D physics based game that creates a 2d circle on mouse click however when it spawns the plane is always side on to the camera. The other problem i encountered is that using LookAt does not allow me to dictate assign the camera since i am using a prefab.

Any ideas?

Experiment with Vector3.back and Vector3.forward, it’ll be one or the other based on how you’re executing it.

I had the same issue. To make the billboard face camera script work properly for me, I had to change my script to:

transform.LookAt(transform.position + Camera.main.transform.rotation * Vector3.down,
		                 Camera.main.transform.rotation * Vector3.back);

The above script was still making the plane tilt down at me when I looked up and tilt up when I looked down. I didn’t like this so I changed my approach to:

using UnityEngine;
using System.Collections;

public class FaceCamera : MonoBehaviour
{	
	GameObject player;

	void Start()
	{
		player = GameObject.FindGameObjectWithTag("Player");
	}

	void Update()
	{
		transform.localEulerAngles = new Vector3 (90, player.transform.localEulerAngles.y - 180, 0);
	}
}

With the above script the billboard only rotates on the y axis relative to the localEularAngle of the players y axis which is the parent to the main camera.

Although, I just reread the OP and it looks like you just need to change the rotation of the plane that instantiates to face your camera…

Set a Quaternion.Eular(x, y, z) variable to 90 degrees in whichever axis you require to make the object face the camera, then use this Quaternion variable in the rotation field when you instantiate your object on click.

If the ray code is:

Ray ray = new Ray (Camera.main.transform.position, Camera.main.transform.forward);

You could rotate it to the camera’s origin forward (or whatever you shot the ray’s from foward) with the variable:

Vector3 rayPos = -ray.direction.normalized;

For my code, I instantiate a plain (that is default vector3.up) and rotate it with:

Quaternion.FromToRotation(Vector3.up, rayPos)