• 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 GBFM · Feb 10, 2014 at 02:13 AM · rotationquaterniongravityangle

Rotation according to gravity

I am trying to make my transform always have its bottom part point to where the gravity vector is pointing (like doing transform.up = -gravity) and to do so I would like it to rotate around its Z axis until it is in the correct position.

I don't have much experience with quaternions, so any idea would be appreciated even if it's just a theoretical one.

Comment
Add comment · Show 5
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 robertbu · Feb 10, 2014 at 02:18 AM 2
Share

The absolute easiest way to solve this problem is to make the part of the object you want align with gravity the 'front'. That is the side of your object that is facing towards positive 'z' when the rotation is (0,0,0). If you do that you can use either of these:

 transform.rotation = Quaternion.LookRotation(gravity, transform.right);  

Depending on how you've organized things, you may want transform.right rather than transform.up.

Show more comments
avatar image GBFM · Feb 10, 2014 at 11:13 PM 0
Share

Thanks, if I can't find a better solution by tomorrow, I guess I will have find a way to do it with LookRotation.

avatar image AdamScura · Feb 11, 2014 at 08:58 AM 0
Share

Sorry for not understanding your question the 1st time. I deleted my previous answer and posted a new one below.

1 Reply

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

Answer by AdamScura · Feb 11, 2014 at 08:57 AM

If I understand your question, you want to rotate an object around it's local Z axis to align it's local -Y axis with your gravity vector. The problem with LookAt() is that it reverses the priorities of the Z and Y axis. It rotates around the local Y axis to align the Z axis with a vector.

We can avoid the problem of axis priorities by giving LookAt() two vectors that are already perpendicular. One of these will be your transform.forward vector. The other needs to be based on the gravity vector, but we need to constrain it to be perpendicular to transform.forward.

So how do we do it?

  1. Project the gravity vector onto transform.forward. This gives you the component of the gravity vector that is acting along the object's local z axis.

  2. Subtract that vector from the original gravity vector. This leaves you with the component of the gravity vector that is acting in the object's local x/y plane.

  3. Make your object LookAt it's own transform.forward, specifying the calculated vector as the up direction.

Here is the code:

 Vector3 gravityAlongLocalZAxis = transform.forward * Vector3.Dot(gravity, transform.forward);
 Vector3 gravityInLocalXYPlane = gravity - gravityAlongLocalZAxis;
 transform.LookAt(transform.forward, -gravityInLocalXYPlane);

If you want to learn more about how I did the calculation, check out the following links:

Vector Projection

Vector Addition & Subtraction

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 GBFM · Feb 11, 2014 at 04:52 PM 0
Share

Thanks for the really descriptive answer, that is exactly what I needed. Also thanks for adding those links, I will definitively look into those.

avatar image AdamScura · Feb 11, 2014 at 11:53 PM 2
Share

@GBF$$anonymous$$, I just found the official Unity docs for vector math! Here are the pages that are relevant to your solution:

https://docs.unity3d.com/Documentation/$$anonymous$$anual/UnderstandingVectorArithmetic.html https://docs.unity3d.com/Documentation/$$anonymous$$anual/AmountVector$$anonymous$$agnitudeInAnotherDirection.html

There is a total of four articles if you want to become a vector Guru. You can find the rest here:

https://docs.unity3d.com/Documentation/$$anonymous$$anual/VectorCookbook.html

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

How to get Ray rotation from direction 0 Answers

How to Constrain Quaternion Rotations Over Time? 1 Answer

Quaternion.FromToRotation misunderstanding 2 Answers

quaternion.eulerAngles 1:1 angle conversion 1 Answer

Calculate rotation angle for two side of cube ( like dice ) from Quaternion.identity 0 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