I noticed that I can set a “slopeLimit” to the character controller, so I was wondering if I could do something similar where by the higher the gradient of the slope the more the character is slowed down. And if the character is moving down the slope then their speed gets quicker with increased slope gradient.
I should be able to figure out how to do this myself if only I knew how to get the angle of the floor the character controller is stood upon. Any pointers?
You can send a raycast down from the player’s position to the ground. The RaycastHit object returned by the raycast has a variable called “normal” - this is the normal vector of the surface at the point where the ray hit. You can get the angle between the normal and the character’s local up direction using Vector3.Angle - you can use this to determine the speed of the character down a slope.
if(slopeVsMovement<0f, inputMagnitude*slopeFactor*walkRun,if(slopeVsMovement>0f AND inputMagnitude>0f, Mathf.Clamp01(inputMagnitude*walkRun+speedUp*(1-slopeFactor)),inputMagnitude*walkRun))
where walkRun is mode max 0.5 when walk and 1f when run.
slopeVsMovement is dot product with value from -1 to 1 depending if transform.forward is opposite direction of slope or same direction (how near).
speedUp is arbitrary value, let say 0.4 which speed up ur character when you walk down the slopeFactor is normalized value 0 to 1, where values near to 1 mean less inclination