Slopegrade in percent from rotation of the x axis

Hi,
i try to get the slopegrade in percent from rotation of the x axis.
I get values from:

slopeGrade = Mathf.Atan(transform.localRotation.x) * Mathf.Rad2Deg *(-1);

But is this correct?, i am really out of mathematics and the things called numbers,
Or is there another way to get the slopegrade in percent by roation?

Well, not really ^^. Lets start what you actually mean by “slopegrade in percent from rotation of the x axis”. Since the grade of a slope is a measurement how much tilted a surface is, it’s not clear what you mean by “x axis”. If you want to know how much the local x axis of an object is tilted in relation to the horizonal plane, it would mean you’re interested in the rotation around the z axis. Though maybe you actually meant the rotation around the x axis? So those are two different things and it’s not clear which one you meant.

Anyways your code doesn’t make much sense. The grade of a slope is specified as the tangent of the slope and when expressed in percent it’s simply multiplied by 100. A grade of 0% would equal an angle of 0° while a grade of 100% would equal 45°

Your next issue is that transform.localRotation.x is neither an angle nor a tangent value. Actually it has no direct relation to how much the object is rotated around the x axis. localRotation is a quaternion and the 4 components form a 4d complex number. The x,y and z part represent the sine of half the rotation angle as well as the rotation axis (since it’s a vector). The w component represent the cosine of half the angle. So this makes not much sense.

In regard to euler angles, using individual angles often times does not give you the desired result as euler angles belong together as they are 3 seperate rotations carried out one after the other.

To get the angle in degree of the x axis (rotation around the z axis) you could use

float angle = Mathf.Asin(Mathf.Clamp(transform.right.y, -1, 1)) * Mathf.Rad2Deg;

If you want to get the angle in degree of the z axis (forward axis, rotation around x) you can use

float angle = Mathf.Asin(Mathf.Clamp(transform.forward.y, -1, 1)) * Mathf.Rad2Deg;

Once you have an angle (in degree) you can calculate the grade by using Tan.

float grade = Mathf.Tan(angle*Mathf.Deg2Rad);
float gradePercent = grade * 100f;

Though I’m wondering why you specifically want to get the grade. The grade has not a linear relationship with the slope angle and angles greater than 45° would quickly explode towards infinity (which is 90°).

ps:
Note that my two examples calculate the grade of a single axis / line whereas grade is usually used to describe the slope of a 2d surface. If you want to get the slope of the x-z-plane (which can be rotated around x and / or z) you would use

float angle = Mathf.Acos(Mathf.Clamp(transform.up.y, -1, 1)) * Mathf.Rad2Deg;

oh thats a great explain for me.

i thought: -25 degree is this?:
and with this i can calculate the percent of 360°?

how ever, it looks and i dont now why it not works:
What i current use is that i locked the rotation of my player and send a ray to the ground:
slopeGrade = Mathf.Abs(Vector3.Angle(hit.normal, transform.forward)) - 90;

now when i unlock the rotation the value is always 0, makes sense because the angle is always the same.

But this is working really perfect:

 float grade = Mathf.Tan(transform.localEulerAngles.x * Mathf.Deg2Rad);
            float gradePercent = grade * 100f;
            slopeGrade = gradePercent * (-1);
            Debug.Log("Slopegrade" + slopeGrade);
            slopeGrade = Mathf.Clamp(slopeGrade, minValue, maxValue);

Thanks for your help!