Camera Following an Object Like a Billboarding Effect?

I have a plane that I’ve added a custom texture to. It appears as if it’s a light shinning effect or volume light. Only problem is that you can tell once you see the side of the plane.

How can I have my plane rotate like the particle effects do or a line render?

Thanks

Have it do something like

var horizontalLookDirection : Vector3 = Camera.main.transform.position - transform.position;
horizontalLookDirection.y = 0;
transform.rotation = Quaternion.LookRotation( horizontalLookDirection );

Look at the camera but only rotate along the world-up axis.

Thanks but this doesn’t work…I add it to the plane right? because it just makes the plane 0* horizontal

At first glance at least, Vicenti code looks correct to me. What does ‘plane 0* horizontal’ mean exactly?

“0 degrees” lol it’s complete horizontal not vertical, it just changed the direction of the object when I need it to stay in the same spot. I just want it to rotate to the direction of the camera so it appears to have a billboarding effect.

Are you using the built-in plane primitive? It lies in the XZ plane, so if you are using it, that’s probably why the object appears to remain horizontal. To fix this, you can instead attach the plane to a child object of the object to which the billboard script is attached, and then assign the child object an x rotation of +/- 90 degrees. (There are other solutions to the problem as well, such as using a ‘quad’ model that lies in the XY plane or setting up the billboard transform differently.)

var forceUpVector : Vector3 = Vector3.up;
...
var horizontalLookDirection : Vector3 = Camera.main.transform.position - transform.position;
horizontalLookDirection.y = 0;
transform.rotation = Quaternion.LookRotation( horizontalLookDirection, forceUpVector );

Set forceUpVector in the inspector to be (1,0,0) or (0,0,1) and one of those ought to orient the plane correctly. LookRotation will try to put the forceUpVector axis along World Up (so you can choose what vector is “up” for your looking).

The parent-child relation above will work too, but I’m not sure if one or the other is better (two gameobjects vs. using a different vector).

Thanks guy, but I’ve tried all of this and no luck and Vicenti I messed with all the directions on the script and it still didn’t work…I don’t know what else to do…any suggestions?

I just tried the parenting method I mentioned earlier and it worked fine - instant axial billboard :slight_smile:

If it’s not working for you, you must have something set up incorrectly. Maybe you could post your current code and describe how your objects are set up (or maybe post a screenshot from the editor). Whatever’s wrong, I’m sure it’s an easy fix.

Alright cool thanks. Well what code am I supposed to be using? I don’t know how to set everything up like you did.

Here are the steps (off the top of my head):

  1. Put the code posted earlier by Vicenti:
var horizontalLookDirection : Vector3 = Camera.main.transform.position - transform.position; 
horizontalLookDirection.y = 0; 
transform.rotation = Quaternion.LookRotation( horizontalLookDirection );

In a script.

  1. Create a new game object, position it wherever you want the billboard to be, and add the script to it.

  2. Create a built-in ‘plane object’. Reset its transform, and then set the X rotation to + or - 90 (if the shader is two-sided it won’t matter which, but otherwise you can just try both).

  3. Remove the collider from the new game object (assuming you don’t want things to collide with the billboard).

  4. Drag the new game object onto the first game object you created.

Unless I missed a step or messed something up, that should do it. If that doesn’t work, post back with details about how it’s not working.

Thanks, but I tried everything exactly as you did it and it still won’t work. Plus I’m getting an error on the code that could be making it so it’s not working properly…

UnityException: You are not allowed to call this function when declaring a variable.
Move it to the line after without a variable declaration.
If you are using C# don’t use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function.

UnityEngine.Camera.get_main ()
lookatcamera…ctor () (at Assets\Materials\lookatcamera.js:1)

That error actually tells you what to do and you didn’t do it? xD

var horizontalLookDirection : Vector3;
horizontalLookDirection = Camera.main.transform.position - transform.position;

Also, that needs to be in an Update() function, not just lying around in a class. Otherwise it’ll run once, if at all, and that’s not what you want.

I don’t really know how to program and it’s still not working…I didn’t get an error code this time though. What do I have to do with an update() function. Thanks for your help though I appreciate it

Can you post your ‘billboard’ script in its entirety?

I just used exactly what was posted above…

var horizontalLookDirection : Vector3; 
horizontalLookDirection = Camera.main.transform.position - transform.position;

Is there more I’m supposed to do to the code?

That’s not exactly what was posted above (take another look).

Is that the entire script? Are you getting any compilation errors?

Although I don’t have any links handy, I’m pretty sure there are some introductory tutorials on UnityScript available. At the very least, you should be able to find some useful into here. In any case, you might want to work through some tutorials and example projects to get some of the basics down, as it’ll make stuff like this a lot easier.

I tried this code:

var forceUpVector : Vector3 = Vector3.up; 
... 
var horizontalLookDirection : Vector3 = Camera.main.transform.position - transform.position; 
horizontalLookDirection.y = 0; 
transform.rotation = Quaternion.LookRotation( horizontalLookDirection, forceUpVector );

And this code:

var horizontalLookDirection : Vector3 = Camera.main.transform.position - transform.position; 
horizontalLookDirection.y = 0; 
transform.rotation = Quaternion.LookRotation( horizontalLookDirection );

They work but they don’t do what they’re supposed to do. I know the basics of programming I just don’t get why this is working for you and not me…

You need an Update() function.
Update runs each frame; I have no idea what happens with code that’s lying around outside functions but it seems like it doesn’t work. :slight_smile:

var forceUpVector : Vector3 = Vector3.up;

function Update() {
  var horizontalLookDirection : Vector3 = Camera.main.transform.position - transform.position;
  horizontalLookDirection.y = 0;
  transform.rotation = Quaternion.LookRotation( horizontalLookDirection, forceUpVector );
}

If they work but they don’t do what they’re supposed to do, they don’t work. Telling us exactly what happens (“it doesn’t move,” “it spins wildly”) is much more likely to get a quick answer than something vague like “it doesn’t work.” :slight_smile:

I get it know thank you very much and it worked.