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 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.

avatar image GBFM robertbu · Feb 10, 2014 at 03:25 AM 0
Share

I already thought about doing that but doing it would mean changing parts of the project that are already done and are fully functional. It also seams a bit of a work around ins$$anonymous$$d of a actual solution.

avatar image robertbu robertbu · Feb 10, 2014 at 05:17 AM 2
Share

I'm not an expert on Unity rotations, but the only two ways I know of that do an arbitrary 3D rotation and also allow you to specify the preferred axis of rotation is Quaternion.LookRotation() and Transform.LookAt(). It is possible to do a combined rotation. That is to use Quaternion.LookRotation() to get the rotation and then to combine it with another rotation to get the orientation right. If the situation has restrictions (say it is 2D for example), then there are other ways to approach the problem.

I'm converting my answer into a comment so that it will be more likely that you will get another answer.

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.

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

Rotating sprite through touch (storing current rotation) 0 Answers

How can I multiply the rotation of a mirrored object? 1 Answer

Rotation approximation issues using Quaternion.LookRotation 0 Answers

Bone Rotation on X (or any) axis Issues 1 Answer

Object makes random jumps in rotation 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