- Home /
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.
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.
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.
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.
Thanks, if I can't find a better solution by tomorrow, I guess I will have find a way to do it with LookRotation.
Sorry for not understanding your question the 1st time. I deleted my previous answer and posted a new one below.
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?
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.
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.
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:
Thanks for the really descriptive answer, that is exactly what I needed. Also thanks for adding those links, I will definitively look into those.
@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