- Home /
Tweaking Sprite Billboard
Hi. I have a pretty in depth but what I think is a simple question to answer here. I am using Unity for 3D game development, but with sprites. I am using the simple billboard code here which I got online:
using UnityEngine; using System.Collections; public class Billboard : MonoBehaviour { void Update () { Vector3 dir = transform.position - Camera.main.transform.position; dir.y = 0.0f; transform.rotation = Quaternion.LookRotation(dir); } }
Whenever I use this script on my sprites, it works well. I only have one small, but annoying issue. When I collide with the sprite, and continue walking into it, it kind of spins to the side and I walk by it. Normally in games, the sprites just stays facing perfectly toward you.
Here is a quick Youtube video I uploaded of the problem in action:
http://www.youtube.com/watch?v=Oy89WT2HxhQ&feature=youtu.be
(Please, ignore the gun clipping in stuff. This is a test project and I have not done the second camera to render it!!)
So, if anyone could help me out here, I would immensely appreciate it! Thanks!
Answer by whebert · Aug 29, 2013 at 09:46 PM
Try this:
Vector3 dir = Camera.main.transform.forward;
dir.y = 0.0f;
transform.rotation = Quaternion.LookRotation(dir);
The only problem with @whebert's code is the '-1'. Remove it, and this solution should work as well.
Ah, yes, sorry. I usually setup any billboard type game objects so that the front side of the texture is pointing in the positive Z direction, or transform.forward. $$anonymous$$akes it easier to deal with in my opinion. I should've noticed by your code yours wasn't setup that way.
But as @robertbu pointed out, the fix would be easy. I'll remove the -1 above.
Thanks! I will try your's out when I have the chance today!
Thanks to both of you for helping me out.
Answer by robertbu · Aug 29, 2013 at 10:37 PM
void Update () {
transform.forward = Camera.main.transform.forward;
}
Setting 'y' to 0 is only necessary if you are tilting the camera up/down.
This worked well, solving the issue I was inquiring about. But, if I look down / up the billboard will move up and down too... As in a vertical latter will appear horizontal facing the camera. Any way to fix this but keep it working the way I like (not sliding to the side when collided with)?
Thanks!
This should fix the look up/down problem;
void Update () {
dir = Camera.main.transform.forward;
dir.y = 0.0;
transform.forward = dir;
}
Note if you allow the camera to look straight up or down, 'dir' will get set to (0,0,1), since you cannot have Vector3.zero for a direction.
Thank you! And thanks for seeing how to fix whebert's code as well!
Your answer
Follow this Question
Related Questions
confusion with sprites 0 Answers
C# GUI draw texture sliced 1 Answer
not sure how to use 2d sprites for my problem. 1 Answer
Checking for an object at a certain position 2 Answers
Multiple Cars not working 1 Answer