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
1
Question by Aggressor · Feb 28, 2015 at 05:42 PM · uitransformlookat4.6worldspace

UIText Inverted When Looking At Camera

I have a very simple script:

 void Update()
 {
     if(camera == null)
     {
         camera = Camera.main;
     }
     transform.LookAt(camera.transform);
 }

Its meant to make a UI element always look at the camera. I created a worldspace text, and I added this script to it.

It looks normal in the scene view before I hit play, but when I run the game, the text is inverted. It does look at the camera but its look at it with a 330* rotation which makes the text inverted!?

alt text

The only way I can get it to look normal is if I set the scale value of the x to be -x. Doing this 'fixes' the problem but Im curious why its wrong in the first place.

Do you know why my ui text is always inverted when attempting to face the camera?

facing.png (24.7 kB)
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
13
Best Answer

Answer by Mmmpies · Feb 28, 2015 at 07:13 PM

Like a guard who's been paid not to notice a robbery you just need to look in the other direction ;¬)

 transform.rotation = Quaternion.LookRotation(transform.position - camera.transform.position);

That should work but let me know if not.

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 Aggressor · Feb 28, 2015 at 08:02 PM 0
Share

First, a big thank you this DID work. Do you know why my logic was not working before? Shouldnt transform.lookat be doing this for me?

avatar image Mmmpies · Feb 28, 2015 at 08:13 PM 1
Share

Nothing wrong with your code, the canvas's all start out at the edge of the terrain looking into the terrain I suspect this is why we always end up looking at the back of the canvas, even world space canvases, when we view it with the game camera.

To fix this we just reverse the lookat logic and effectively get the canvas to face away from it's default position.

Don't know why Unity did it this way but if you look in this tutorial:

WorldSpaceUI

You can see they have to manually rotate the canvas, and that's Unity tutors so it's got to be the default behavior.

I use that code to get animals to run away from the players position when they get too close to them but it works just as well with a camera and the UI.

avatar image Aggressor · Feb 28, 2015 at 08:32 PM 0
Share

Brilliant thanks, I had watched that tutorial but I think I missed that part. But yes very counter-intuitive. Thank you again for a very clear explanation, I was pulling my hair out haha!

avatar image
5

Answer by lodendsg · Oct 22, 2017 at 11:47 AM

Just for note when you want a UI element to face the same direction as a given camera its better to say

transform.rotation = camera.rotation

If you set it to 'look at' the camera its going to look at the camera's pivot point and depending on your canvas distance from camera and how far from center screen the element is rendering that will be off slightly. Also its unnessisary and takes more processing than simply copying the cameras world rotation though neglagably so.

So the question is do you want your element to 'face' the camera pivot or do you want it to face toward the same direction the camera is seeing.

Comment
Add comment · 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

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

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

Related Questions

Converting UI RectTransform to Screen Pixel and vice-versa 5 Answers

[Unity 4.6 Beta] Rect Transform Position (New UI System) 7 Answers

LookAt fails at close range. 1 Answer

(c#) Photon networking, get players in room? 1 Answer

transform.LookAt gives a twist 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