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