- Home /
Camera facing billboard !
I want a billboard to always face an object (a camera) and I see how to do it documented in several places but none seem to work for me and I can't figure out what is wrong.
Here is my code:
{
public GameObject TargetGO;
void Start() { }
void Update()
{
transform.LookAt(transform.position + TargetGO.transform.rotation Vector3.back, TargetGO.transform.rotation Vector3.up);
}
}
So I drop another GameObject into TargetGO so I can watch the billboard orient.The billboard is always facing 'up' compared to the GameObject.
Tried a simple transform.lookat(TargetGO.transform.position) and this also has the billboard always point up. with billboard at 0,0,0.
What am I doing wrong????
Answer by FatWednesday · Sep 11, 2012 at 01:00 PM
The following script should do it, it looks like you are over complicating your LookAt perameters. when using the Transform.LookAt function, it automatically takes the transforms position into account, so you only need to provide target vector, not a relative one.
public class MyClass: MonoBehaviour
{
Transform MyTransform;
Camera Cam;
// Use this for initialization
void Start ()
{
MyTransform = transform;
}
// Update is called once per frame
void Update ()
{
MyTransform .LookAt(Cam.transform.position, upVector);
}
}
and obviously you want to replace upVector with whatever you actually want to be your up vector.
This script didn't do it. I think there is something messed up about the initial orientation of my billboard. I have created it and its rotation is 0,0,0.
I used the smoothlookat script (standard assests) and put it in my billboard prefab. The billboard continues to point (face) the upvector of the object (I substitued an gameobject for the camera so I could watch the billboard). As my object flys around in 3d space and changes its up vector so does my billboard to match the upvector of the gameobject.
Have I missed something in setting the prefab up?
Depending on the local rotation of your object, you might have to append additional rotation transforms to your object. For example, using the built-in Plane object my rotation has to look like this.
$$anonymous$$yTransform.rotation = Quaternion.AngleAxis(90, $$anonymous$$yTransform.right) * Quaternion.LookRotation(Camera.main.transform.position, Camera.main.transform.up);
Thanks. But do all folks who do camera facing billboard have to do this? I am not doing anything special. I want to create a plane and have it face the camera where it is in 3d space.
So there must be a "easy" way to do this that perhaps I am missing.
I'll try your suggestions. Thanks again!
Answer by Jessy · Sep 11, 2012 at 01:47 PM
Billboarding is better done in a shader than a script.
http://en.wikibooks.org/wiki/Cg_Programming/Unity/Billboards http://en.wikibooks.org/wiki/GLSL_Programming/Unity/Billboards http://forum.unity3d.com/threads/109111-GLSL-Animated-sprite-shader-billboard-questions
Your answer
