- Home /
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?
Answer by tonycoculuzzi · Dec 06, 2011 at 02:04 AM
http://www.unifycommunity.com/wiki/index.php?title=CameraFacingBillboard
Experiment with Vector3.back and Vector3.forward, it'll be one or the other based on how you're executing it.
function Update(){ transform.LookAt(transform.position + Camera.main.transform.rotation Vector3.back,Camera.main.transform.rotation Vector3.up); }
This should work as a JavaScript translation
Sorry I forgot to mention im using javascript. Is there a similar tutorial for java?
Thanks for the help so far
(also sorry for posting answer as another question first ><)
Haha no worries, like I said, I'm not one to care. I was just letting you know for future reference. ;)
Let me know if that translation works for you.
Unfortunatly it doesnt, It seems to keep the plane edge facing the camera. I tried changing the .front to .back like you mentioned but it made no difference.
I dont know if it makes any difference but i have removed the mesh collider and applied a cylinder collider to allow for more accurate physics on the 2d view
Nope, it shouldn't make a difference what collider is on it. $$anonymous$$ake sure the script is on the object you would like to face the camera, and not the whole prefab.
If that requires you placing the code in a new class and placing the class on JUST the plane, I recommend doing that. Either that, or reference the plane in your code and ins$$anonymous$$d of transform.LookAt, use plane.transform.LookAt where "plane" is the variable referencing your plane GameObject.
Answer by MMOERPG · May 27, 2014 at 08:54 AM
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.
Answer by Unity_scat · Nov 13, 2016 at 10:24 PM
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)
Your answer
![](https://koobas.hobune.stream/wayback/20220613061820im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Firing a rocket - flight problem 2 Answers
Need help putting an object at a point based on screen coordinates 1 Answer
Showing what the Camera sees on a plane 1 Answer
Get transform of instantiated Prefab; Help appreciated. 1 Answer
Can't set camera target after instantiating a prefab(like respawn) 0 Answers