Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
7
Question by raul corrales · Mar 16, 2011 at 06:43 PM · cameraspritefacesee

how I can create an sprite that always look at the camera?

how I can create an object that always look at the camera?

Like this tree:

alt text

Thanks in advance!

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

7 Replies

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

Answer by DaveA · Mar 16, 2011 at 08:47 PM

using UnityEngine;

public class Billboard : MonoBehaviour { void Update() { transform.LookAt(Camera.main.transform.position, -Vector3.up); } }

Call it Billboard.cs and drop it on any object you want to be a billboard and/or make a prefab.

Comment
Add comment · Show 9 · 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 raul corrales · Mar 16, 2011 at 09:03 PM 0
Share

thanks newly dave

avatar image DaveA · Mar 16, 2011 at 09:05 PM 1
Share

No prob. Note: you may need to not negate Vector3.up depending on your mesh. I used a simple quad I made in Blender that might have been backward. So if you don't see anything, it's probably facing the wrong direction, so take that - sign out.

avatar image impurekind DaveA · Aug 04, 2018 at 10:28 PM 0
Share

I think I'm having this problem but removing the $$anonymous$$us sign didn't fix the issue.

Any other suggestions?

Note: I'm using a Sprite Renderer just in case that makes a difference.

avatar image anomalous_underdog · Apr 05, 2011 at 05:11 AM 2
Share

Yup, I needed to do that. $$anonymous$$ost likely for others too, just remove the negation on the Vector3.up

avatar image marsbear · Aug 06, 2012 at 10:40 PM 0
Share

Shouldn't that be put in LateUpdate? Otherwise your billboard might flicker slightly depending on if and how you move it.

avatar image Sarrixx · May 16, 2020 at 02:45 PM 4
Share

Try this for those wanting the billboards to stay 'upright' on the y axis.

 public class CameraBillboard : $$anonymous$$onoBehaviour
 {
     private void LateUpdate()
     {
         transform.forward = new Vector3(Camera.main.transform.forward.x, transform.forward.y, Camera.main.transform.forward.z);
     }
 }
Show more comments
avatar image
19

Answer by JustinReinhart · Jul 08, 2016 at 02:21 PM

I spent some time researching the issue of billboard sprites. I personally believe that the correct course of action is not to just perform a Transform.LookAt, but to actually align all billboard sprites to point the same direction that the camera is pointing. If you simply have each sprite look at the camera, multiple billboards across your screen will create a curve around your camera and they jut into each other if they overlap; This is because each billboard sprite would exist in 3D space at slightly different angles. Worse still, objects on the edge of the screen get warped like a funhouse mirror. Therefor aligning seems to be the way to go.

 MyTransform.forward = MyCameraTransform.forward;

I also agree with marsbear that there may be a benefit to putting the code in LateUpdate.

Pictures and reference code available here: http://www.justinreinhart.com/2016/07/08/pixel-perfect-billboard-sprites/

Comment
Add comment · Show 9 · 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 tanoshimi · Jul 08, 2016 at 02:54 PM 1
Share

Or just:

 myTransform.forward = -myCameraTransform.forward;
avatar image JustinReinhart · Jul 09, 2016 at 10:39 AM 0
Share

Great optimization tanoshimi! I'm actually using $$anonymous$$yTransform.forward = $$anonymous$$yCameraTransform.forward; now specifically because the negative value was flipping the sprites.

avatar image tsemple · Feb 14, 2017 at 01:07 AM 0
Share

In general I'm not sure this is what you want to do. This makes the sprite face the same direction as the camera which isn't the same as towards the camera. If look around especially with the sprite as the edge of the view window, it will seems like it's rotating ins$$anonymous$$d of fixed towards the viewer.

avatar image kamran-bigdely · Sep 13, 2017 at 11:47 PM 0
Share

There is a subtle issue with this approach. If the camera is close to the billboard but it's not pointing at it (but still in the field view), you would see the billboard is not 100% facing the camera. It's facing the camera like 80%. To make it 100% facing camera, use LookAt() method.

avatar image Bunny83 kamran-bigdely · Sep 14, 2017 at 12:43 AM 4
Share

There are generally those two billboarding techniques used in games. $$anonymous$$ost billboards actually use this approach (for example Quake3 for any particles / sprites like explosions, smoke, plasma. Also Source engine HL2). This is usually the cheapest solution as only the camera direction is required. This is especially useful for shader billboarding solutions.

The second approach is actually let the object face the position of the camera. This is also used in Quake 3 for the rail gun beam (the rotation around the beam direction).

Both techniques have advantages and disadvantages.

Cam forward

  • pro: usually the fastest

  • pro: It's insensitive to player movement.

  • con: When you rotate the camera you will notice that the billboard object rotates with you which can look weird at certain angles. However when used for fast moving objects you usually can't notice it.

Cam lookAt

  • pro: It's insensitive to camera rotation. It's best used for distant objects where the player never gets too close.

  • con: It's sensitive to camera / player motion. This can look also quite weird when you closely run past a billboard object its rotation is quite noticable depending on the camera field of view.

Which technique is better depends on how long is the object visible and what kind of movement does happen more often, camera rotation or camera movement.

The point of a billboard is not to "make it face the camera", but to give the illusion of an actual object, even it is just a flat sprite. The cam forward vector method does not change the appearance when you just walk by such a billboard object. But it looks strange when you rotate the camera.

avatar image impurekind · Aug 04, 2018 at 10:11 PM 0
Share

Well, DaveA's code didn't work for me but your code in the link worked out the box, which is exactly what I like to see as a total noob just trying to figure this stuff out. So thanks. :D

Edit: Ah, one issue: The sprite tilts backwards and forwards towards the ground if I get close to it and look up and down, so it kinda looks like it's trying to lie down. lol Is there I way I can stop it doing that?

avatar image JustinReinhart impurekind · Aug 04, 2018 at 11:45 PM 0
Share

@impurekind To get the object to stop laying down: First, the reason it is laying down is because it is copying the camera's rotation exactly. If the player looks down they are rotating the camera to look down. So the object will rotate to match. Theoretical solution: After the line of code that copies the 'transform.forward' of the camera to the object, in the next line of code you need to immediately set the object's rotation so that it is not tilted on an axis that allows it to lay down. Basically, between frames that the player can see, you would be setting the rotation a second time. First a copy. Then a slight adjustment so it is standing up. And then the next frame is drawn.

Alternatively, but still the same concept, it sounds like what you want is for the objects to only rotate around the Y axis (to turn left and right around a vertical pole, but never tilt forward or backwards.) You would want to replace my code with a line of code that only copies the Y rotation of the camera but does not attempt to copy or change the rotation on any other axis. I'll leave that up to you to figure out cause it should be a fairly simple exercise for a self-described beginner. (thumbs up) Have fun.

avatar image impurekind JustinReinhart · Aug 05, 2018 at 09:28 AM 0
Share

Well, thanks for the tip but the entire point is I cannot figure this out because I simply do not understand c# (or any of Unity's specific variations/uses of it). It is beyond confusing, convoluted and unintuitive to me (as is much of Unity). And it's counter-productive to take multiple hours to try and fix every single tiny issue that comes up when it could be more effectively solved in a few $$anonymous$$utes via some actual implementable suggestion from people who inevitably know better due to many more years of experience (with both Unity and C#).

I spent three hours on this one little issue last night, trying multiple different potential solutions I found online (ones provided to other people covering this general issue), as well as looking up Unity's documentation and watching various YouTube examples, and nothing I tried worked. Every single thing I tried myself just made the sprite appear to disappear entirely. And the whole point was in getting someone to provide me a solution to the issue now rather than wasting further hours/days on me trying to figure it out blindly for no good reason when the solution exists and other people in the community already know it.

I can't go wasting hours on every issue that could be solved in $$anonymous$$utes if someone who know's the solutions could help me resolve them properly there and then (which will usually be the case because they have far more experience and understanding of both Unity and C#), and if they could simply provide me with a simple working suggestion then I will then implement it, and, because it actually works, I can trust it's not just me faffing around in the dark co$$anonymous$$g up with bad methods to solve the issues and learn from it for next time.

Basically, whenever I try my own "solutions", they do not work--I simply do not understand enough to fix these issues myself, and it's dumb of me to spend years learning Unity and C#, stalling any current progress until then, to fix this (and every other of the $$anonymous$$ANY issues that come up daily) years down the line when it could be fixed right now--and you guys have the solution right now. All I'm asking is for someone to provide that solution, which I will then implement now and also learn better from it because I'm doing it correctly.

PS. It's strange to me how people say "have fun" with stuff like this--because coding is the least fun thing I can imagine. This is not fun for me--at all. I literally hate coding; but it's something that unfortunately needs to be done to get my game to even work.

$$anonymous$$aybe you can tell I'm not having fun here--and it would simply be nice to reduce the amount of frustration and stress as much as possible really.

Show more comments
avatar image
2

Answer by idbrii · Apr 04, 2018 at 11:24 PM

If you want your billboard to stay upright so it doesn't tilt back to look up at the camera when it approaches, you need to modify DaveA's solution so your target position has no height changes:

 public class Billboard : MonoBehaviour
 {
     void LateUpdate()
     {
         var target = Camera.main.transform.position;
         target.y = transform.position.y;
         transform.LookAt(target);
     }
 }
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 impurekind · Aug 04, 2018 at 11:30 PM 0
Share

DaveA's solution works for me other than tiling back part, but as soon as I try any of your other guy's suggestions, including the various fixes for the sprite not being visible, I can't see the sprite at all.

Do you have any idea why this is and how to fix it?

Note: I'm using a Sprite Renderer if that's important here.

avatar image idbrii impurekind · Aug 19, 2018 at 01:27 AM 0
Share

@impurekind: I'd guess because your sprites are backwards and you're seeing the backface. Try moving the scene camera around to the other side and see if you can see it from there.

Flipping the sprite is what DaveA's -Vector3.up was doing.

avatar image Sarrixx · May 16, 2020 at 02:43 PM 0
Share

Simplified to 1 line of code, modification of Justin's code above.

 public class CameraBillboard : $$anonymous$$onoBehaviour
 {
     private void LateUpdate()
     {
         transform.forward = new Vector3(Camera.main.transform.forward.x, transform.forward.y, Camera.main.transform.forward.z);
     }
 }
avatar image
1

Answer by eppz · Sep 21, 2018 at 12:05 PM

Just use the quaternion rotation of the camera for orienting sprite.

As simple as:

 sprite.transform.rotation = Camera.main.transform.rotation;

Comment
Add comment · Show 1 · 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 summerian · Sep 25, 2018 at 02:11 PM 0
Share

Great and simple solution. Thanks.

avatar image
0

Answer by Meltdown · Mar 16, 2011 at 07:31 PM

The technique you are referring to is known as 'billboarding' On the Unity terrain editor you can create trees and set their billboarding settings.

Comment
Add comment · Show 1 · 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 raul corrales · Mar 16, 2011 at 08:40 PM 0
Share

ok thanks for reply , and in a individual plane??

  • 1
  • 2
  • ›

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

19 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 avatar image avatar image avatar image

Related Questions

2D - FollowCam causes player sprite to disappear 0 Answers

How can I make a layer black? Unity 2D 1 Answer

Unity 2D: Pixel Perfect Camera + Resolution Help 0 Answers

How to make camera position relative to a specific target. 1 Answer

Question about sprite size and camera size 2 Answers


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