Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Evan Stallings · Aug 26, 2013 at 01:32 AM · c#2dsprites

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Evan Stallings · Aug 30, 2013 at 12:22 AM 0
Share

That didn't work. Thanks though!

avatar image robertbu · Aug 30, 2013 at 02:59 AM 0
Share

The only problem with @whebert's code is the '-1'. Remove it, and this solution should work as well.

avatar image whebert · Aug 30, 2013 at 03:59 AM 0
Share

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.

avatar image Evan Stallings · Aug 30, 2013 at 10:53 AM 0
Share

Thanks! I will try your's out when I have the chance today!

Thanks to both of you for helping me out.

avatar image Evan Stallings · Aug 30, 2013 at 11:38 AM 0
Share

Worked perfectly now, thanks!

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Evan Stallings · Aug 30, 2013 at 12:24 AM 0
Share

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!

avatar image robertbu · Aug 30, 2013 at 02:58 AM 0
Share

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.

avatar image Evan Stallings · Aug 30, 2013 at 10:54 AM 0
Share

Thank you! And thanks for seeing how to fix whebert's code as well!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges