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
3
Question by EBR · Jul 19, 2014 at 12:02 PM · lookatlookrotationfromtorotation

Lookat, FromToRotation and LookRotation?

I have been reading the documentation and it isn't clear to me what the difference between them are. What I am trying to achieve is to get the objects Up (Y) axis aligns with the normal of the ground underneath and the objects Right (X) axis aligns points to a target. Thanks for you help :)

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

3 Replies

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

Answer by robertbu · Jul 19, 2014 at 02:21 PM

Transform.LookAt() points the positive 'Z' (forward) side of an object at a position in world space. Quaternion.LookRotation() points the positive 'Z' side of an object in a specified direction. Quaternion.FromToRotation() creates a rotation that from one direction to another direction. Note the use of the positive 'Z' (forward) in the descriptions. The easiest way to make things work is to make sure that you are aligning the positive 'Z' of an object. So if at all possible you should change your setup. That can be done in the modeling program, or that can be done by using an empty game object. Create an empty object. Make the visible object a child at the same position (i.e. local position of (0,0,0). Then rotate the visible object 90 degrees so that the right (x) side of the object is aligned with the positive 'z' (forward) side of the empty game object.
The script goes on the empty game object.

To solve your problem, you need to start with a ProjectPointOnPlane() function:

 function ProjectPointOnPlane(planeNormal : Vector3 , planePoint : Vector3 , point : Vector3 ) : Vector3 {
     planeNormal.Normalize();
     var distance = -Vector3.Dot(planeNormal.normalized, (point - planePoint));
     return point + planeNormal * distance;
 }

 

Then you can do:

 var q = Quaternion.FromToRotation(transform.up, hit.normal);
 transform.rotation = q * transform.rotation;
 var pos = ProjectPointOnPlane(transform.up, transform.position, target.position);
 transform.LookAt(pos, transform.up);

If you want the current 'X' axis without remodeling or using an empty game object, the following additional line should work (untested):

 transform.rotation = Quaternion.AngleAxis(90.0, transform.up) * transform.rotation;

You may need to negate the angle or the axis in this line of code.

Let me go through the code so you understand what is going on. The first line creates a rotation that goes from the current 'up' of the object to the normal of the surface (I'm assuming you got that normal from a Raycast hence the hit.normal in the line of code). The second line rotates the object by that amount aligning the 'up' of the object with the normal. The third line takes the target positition and projects it onto a plane passing through the object's center and aligned with the up of the object. This projection is necessary so that the LookAt() will only rotate on the objects 'y' axis. The fourth line uses LookAt() to align the positive 'z' side of the object with the point that was created by projecting the target position onto a plane. The optional additional line spins the object on its 'y' axis 90 degrees so that instead of the 'z' side pointing at the target, the 'x' side should be pointing at the target. Pretty complicated for doing something that is so simple to describe.

Comment
Add comment · Show 2 · 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 kamran-bigdely · Apr 14, 2016 at 11:49 PM 0
Share

There is no "Quaternion.LookAt()" in Unity and I assume you meant "Quaternion.LookRotation()" ins$$anonymous$$d.

avatar image Bunny83 kamran-bigdely · Apr 15, 2016 at 04:09 AM 0
Share

Of course. LookAt wouldn't make any sense since a Quaternion only represents a rotation. You can't "look at" something without having a position from where you look. I changed it in the answer ^^.

avatar image
2

Answer by andrew-lukasik · Jan 22, 2016 at 03:03 PM

To align something (building or RTS unit etc.) to the ground use:

 ROTATION = Quaternion.LookRotation(
     Vector3.ProjectOnPlane( DIRECTION , SURFACENORMAL ) ,
     SURFACENORMAL
 );
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 Compressed · Aug 16, 2016 at 05:51 PM 1
Share

I have been trying to solve this problem forever! Thank you, this is perfect.

avatar image
0

Answer by dansopanso · Feb 06, 2019 at 02:24 PM

I found this post more or less by accident and it really helped me to solve the problem how to align an object with two different axis!

I had the same problem as the Author since my player is moving around a spherical world and I need to align Objects with the planet surface but still rotate towards the player.

Thank you very much! Since It's a very old post and I needed to change the code a little to get this working I thought I'll post it here. Maybe that helps another noob like me to get this working more quickly ;)

 Vector3 ProjectPointOnPlane(Vector3 planeNormal, Vector3 planePoint, Vector3 point)
 {
     planeNormal.Normalize();
     var distance = -Vector3.Dot(planeNormal.normalized, (point - planePoint));
     return point + planeNormal * distance;
 }

 void rotateToPlayer()
 {
     Vector3 playerPosition = player.transform.position;

     var q = Quaternion.FromToRotation(transform.up, playerPosition);
     transform.rotation = q * transform.rotation;
     var pos = ProjectPointOnPlane(transform.up, transform.position, player.position);
     transform.LookAt(pos, transform.up);
 }  
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

28 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

Related Questions

How to get RigidBodies to LookAt player, while also moving around the surface of a cylinder? 1 Answer

Bullet not moving towards player 3 Answers

LookRotation is Returning Weird Values 0 Answers

Rotation using Unity2D 3 Answers

Camera following/looking at aircraft 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