- Home /
Getting slope angle from normals.
Im making a character controller and i want to be able to control the maximum slope angle the character can climp/walk.
Here ^ for example the player wants to walk up a cliff in the direction of arrow 1, but i need this angle (blue):
So i can check if the angle is smaller than 40(or any other value). I also need this to work in all directions. For example 2nd arrow in picture 1. Hope you understand my question. I googled for a bit but i didnt find anything that i could understand. Also im using raycasting for this. Thanks.
http://docs.unity3d.com/ScriptReference/RaycastHit.html
http://docs.unity3d.com/ScriptReference/RaycastHit-normal.html
http://docs.unity3d.com/ScriptReference/Vector3.Angle.html
angle = Vector3.Angle( hit.normal, Vector3.up );
It works but it always shows the angle in the 2nd pic. I need it to work in all directions, for example if the character would walk down, the angle would be -20, or if the character walked like the 2nd arrow in pic 1. it would return an angle of 14 and so on. I am trying but i just cant figure it out.
Answer by Owen-Reynolds · Jul 03, 2015 at 04:57 PM
To get the slope in the direction you want to move, take the angle between the normal, and your "flat" forward. Zero slope will be 90 degrees.
If you have a character controller, or something that never tips, then can just use player.forward
-- it will always be flat (have no Y.) Otherwise just use Vector3 pf=player.forward; pf.y=0;
. Then just take the angle.
To see how this works, suppose you're walking sideways along a hill (so no slope.) Take one finger as the way you want to move. Take another and tilt it down to be the hill's normal. The 2nd finger is just spinning a little compared to the first, so the angle is still 90. Then redo with finger one for forward into the hill (the xz direction up the hill, but flat, so into the hill.) Compare to that same hill normal, which is now tilted away from your movement finger. Angle more than 90.
You should be able to see the "climb angle" is [angleMath]-90.
Answer by stanogg · Jul 05, 2015 at 06:56 AM
Just got it working, finally. Im actually only 14 and i dont know much about angles. My equation is: ((Vector3.Angle( hit.normal, transform.forward)) - 90). Thanks everyone for the help! :)
Answer by wesjmulder · Feb 28 at 10:29 AM
slopeAngle = Vector3.Angle(slopeHit.normal, Vector3.up);
This should work. If you are using a raycast originating from the center of the player, going down to its feet. To detect slopes. , slopeAngle = Vector3.Angle(slopeHit.normal, Vector3.up); This should work. If you are using a raycast originating from the center of the player, going down to its feet. To detect slopes.