- Home /
billboard sprite along axis
I'm making a sprite that's function is to point towards a location. This it does very well with being parented to an object that sits at that location.
My problem is that this sprite needs to be always facing the camera too.
To put it simply how can I get the sprite to look at the camera while having one axis always pointed at a location?
Answer by robertbu · Aug 01, 2013 at 08:05 PM
Giving a solution to this kind of problem can be difficult because everyone sets up their objects and environment differently. So I'm going to give you one solution, you can play with. If it does not match your environment, you can tell me how/why and I can help you modify it.
Get the CreatePlane script from the Wiki. This script allows the creation of a vertical plane.
Create a vertical plane (for your sprite).
Get the Math3d script from the Wiki. Specifically you want to use the ProjectPointOnPlane() function.
Attach a material with a texture where the pointer is pointing up. I've included one below where the tip of the point ends at the center of the texture (at the default pivot for a plane). There are other ways of solving the pivot point issue.
Attach the script to the plane.
Drag the object to follow onto the 'target' in this script in the Inspector.
public class PointAt : MonoBehaviour { public Transform target;
void Update() { Vector3 normal = Camera.main.transform.position - transform.position; Vector3 totarget = Math3d.ProjectPointOnPlane(normal, transform.position, target.position); transform.rotation = Quaternion.LookRotation (-normal, totarget - transform.position); } }
The concept is that you create two vectors, one between the camera and the pointer. One between the pointer and the position of the target projected onto the plane the sprite will be sitting on. Then LookRotation() takes care of the rotation.
I guess I made that question too simple. ^_^ I already had everything setup I just needed the code/formula for getting it working.
plugged in the script, and must say wasn't what I was looking for, but I like it a lot more than what I was trying to do. Thanks for the help!
Also excellent tutorial for the next person who has this problem!
Answer by LittleDreamerGames · Feb 11, 2021 at 03:51 AM
This is a very old post, but I was looking for a more optimized version of the axis aligned billboard script I came up with, and came across this. It looks like everyone is doing pretty much the same way.
Anyway, because I love puzzles, I wanted to figure this one out and share the result. I think you're looking for something like this:
public class PointerBillboard : MonoBehaviour
{
public Transform target;
void LateUpdate()
{
if(Camera.main)
{
Vector3 up = (target.position - transform.position).normalized;
transform.LookAt(Camera.main.transform.position, up);
}
}
}
I used the arrow shared by robertbu applied to the Unity Quad shape for my tests. It only works with perspective cameras and you have to use a shader that draws back faces (or make your own quad that faces down -z). It also will only work with a camera tagged with "MainCamera".
Here's what it looks like:
Cheers!