# Read the Angle between an Object and a World Axis

Hi again. I need to know the angle of my rigidbody. Is the body of a man that is walking and I need to detect when he is falling far rectificate the forces to keep him up.

How can i read a simple angle or rotation of an object?

1 Like

You can use Vector3.Angle(Vector3 a, Vector3 b) to find the angle in degrees between two vectors. It is always a positive number as it doesnâ€™t take into account the direction of rotation. If you need a positive/negative angle then you will have to do this:

``````public float PosNegAngle(Vector3 a1, Vector3 a2, Vector3 normal)
{
float angle = Vector3.Angle(a1, a2);
float sign = Mathf.Sign(Vector3.Dot(normal, Vector3.Cross(a1, a2)));
return angle*sign;
}
``````

Where â€śnormalâ€ť is the reference Vector you are determining the clockwise / counter-clockwise rotation around.

Thanks Antitheory, but then,

How can I get the Vector3 atached to the Y axis of my Rigidbody?

I need to use its local Vector in the world reference coordinates?

or I only have to use the rigidbody vector as a1 and the world axis as a2? I mean; I need the angle between the Y axis of my Rigidbody,(it changes when it is falling), and the Floor ( is always the same plane ).

Every gameObject has a Transform component which contains the location/rotation data for the object, as well as methods for modifying them. You can access the local axes of the Transform component like this

transform.up //+y axis
transform.forward //+z axis
transform.right //+x axis
-transform.up
-transform.right
-transform.forward

So if you want to find the angle between the floorâ€™s normal (which I will assume is straight up which can be represented by the constant Vector3.up) and the rigidbodyâ€™s UP axis then do:

float angle = Vector3.Angle(Vector3.up, transform.up);

if the angle is zero, then the object is parallel to the floor. However the problem is that your object could be rolled one way or the other, but itâ€™s nose is till pointing toward the horizon.

If you want to find whether or not itâ€™s pointing up or down you can do this:

float angle = 90 - Vector3.Angle(Vector3.up, transform.forward);

Now if angle is zero, it means the objects nose is pointing toward the horizon (but it could be rolled or even upside down). Negative angles are up, positive angles are down.

If you need to know whether the object is rolled left or right (like when a plane is banking)

float angle = 90 - Vector3.Angle (Vector3.up, transform.right);

2 Likes

GREAT. Thanks you very MUCH. This is the best explanation i have seen in this forum this year,
Really, now I can understand angles much better.

My main problem was that I havnâ€™t understand the difference between transform.up and Vector3.up, very exactly. And I didnâ€™t know how to drive a Vector3.angle.

Thanks again.

Iâ€™m eager to get home to test it in my project. I will show you my results.

Vector3.Angle is working :P. Thanks.

Now my proble are the forces. I used forces and position moves on foot when he is falling X angle, but it does a lot of bugs, and physics problems.

Then, my solution for now is increase the foot mass when he is falling. Is like a boxing punch which never falls xD. But it is a temporally solution.

The ecuation is: