I want to make a simple billboarding script that makes a quad follow the camera, but only limit it to the y axis. Currently I am using:
transform.rotation = Camera.main.transform.rotation;
However, this rotates the object on the x and z axes as well. I simply want to constrain it to the y axis but i have encountered some errors when trying to add the y axis.
I tried using LookAt but the object simply faces the camera away.
Any one who can help me achieve what i want?
I used this C# script… add it to the object you want, to follow the camera.
using UnityEngine;
public class Billboard : MonoBehaviour
{
void Update()
{
// look at camera...
transform.LookAt(Camera.main.transform.position, -Vector3.up);
// then lock rotation to Y axis only...
transform.localEulerAngles = new Vector3(0,transform.localEulerAngles.y,0);
}
}
crazy late but , i hate it when i stumble on posts that don’t have the answer I’m looking for. should work with whatever up axis you want, and stays in world space.
Vector3 upAxis = Vector3.up;
transform.rotation = Quaternion.LookRotation(Vector3.Cross(upAxis, Vector3.Cross(upAxis, Camera.main.transform.forward)), upAxis);