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 nerz89 · Mar 18 at 10:02 AM · transform.lookat

Question regarding to transform.LookAt

So, here's the thing. I'm working on a top-down shooter and I've got some enemy prefabs. They have sliders attached to them representing their health. The camera follows the player as I move around, however, the enemy sliders don't follow the rotation of the camera so in many cases the sliders can't really be seen properly. I found a solution to this problem online. It's only one line and I attached to the slider:

transform.LookAt(transform.position + Camera.main.transform.rotation * Vector3.back);

The transform.LookAt itself is pretty straightforward, I have no problem with that. But I can't seem to wrap my head around the main part (transform.position + Camera.main.transform.rotation * Vector3.back).

So basically this little script works perfectly, but what exactly happens here? Why do I need to add up the transform.position and the rotation of the main camera? Then the multiplication with the Vector3.back also baffles me a bit.

I'd like to understand this so any help would be appreciated!

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
4
Best Answer

Answer by Hellium · Mar 18 at 10:17 AM

You need to break the operation down and learn about some linear algebra.

 rotation * direction = direction
 position + direction = position

Thus

 position + rotation * direction = position


  • transform.position = position

  • Camera.main.transform.rotation = rotation

  • Vector3.back = direction


So transform.position + Camera.main.transform.rotation * Vector3.back = position is a point in space


Camera.main.transform.rotation * Vector3.back will take Vector3.back ( (0, 0, -1) ) and rotate it so it is expressed in the camera's space. It is equivalent to the camera's back vector

-Camera.main.transform.forward (notice the - here)


So transform.position + Camera.main.transform.rotation * Vector3.back is the position of the object minus the back vector of the camera.

An image may help understand

alt text


lookat.png (5.0 kB)
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 nerz89 · Mar 19 at 09:02 AM 0
Share

Wow, thank you for the detailed explanation!

avatar image
1

Answer by Captain_Pineapple · Mar 18 at 10:19 AM

Hey there,

My answre is based on the assumption that the line you describe is in a script which is on the gameobject of your "sliders".


So basically what you want to achive ultimately is calculate a position in world space towards which your health bar "looks". This can be achived in multiple ways. What your current approach does is take the Vector (0,0,-1) and it rotates it so that it is the global representation of the cameras back vector. The rotation is done by multiplying it with the cameras rotation. You should get the same result if you write -Camera.main.transform.forward.


By adding the position of your healthbar you create a point in global space in relation to your current health bar. This way you make sure that all of them are rotated the same way.


as some thoughts for further improvements of this: if we assume you'd have a game with 1000 enemys in one area, this approach would kill the framerate on most PCs as you'd create unecessarily many calculations which all should result in the same quaternion. (This is since the direction vector you get from cameraRotation*Vector3.back is the same for all healthbars)

Here you should be able to calculate a quaternion instead:

   LookAtRotation = Quaternion.LookRotation(-Camera.main.transform.forward);

Which you can then access and set as rotation for all of your healthbars and thus don't have to recalculate that each time:

   healthbar.transform.rotation = PlayerCameraScript.LookAtRotation;

(Code above is only pseudo code suggesting how you could approach this in theory)

let me know if something was unclear.

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 nerz89 · Mar 19 at 09:03 AM 0
Share

Thank you for the explanation and also for the alternative method. Works perfectly 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

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

Related Questions

How to make an arrow constantly point at an object without jittering? 1 Answer

Transform.LookAt - Look through Y-Axis? 1 Answer

Unity, Y U IGNOR MY Y? 2 Answers

transform.lookat wrong direction 2 Answers

Upper body look at problem 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