Detect diagonal player movement in a slope

Been trying for a while now to find a way to detect when the player is diagonally acending a slope. I really can’t figure out how to do it. One idea (that is not 100% reliable) is to cast 4 vertical rays (forward, back, left, right) hitting ground. Then check if forward ray is shorter the back and same for left and right. But that don’t really work if your standing near a small bump in the terrain or something. I need this as a modifier when the rigidbody using force to move uphill. As it is now I give the player a force boost depending on the ground angle to give a more fluid movement. But if the player move almost horizontal in the slope the same boost applies and rockets away.

Easiest way is to grab the normal of the ground you’re standing on, and use it to convert your flat directional force vector into one that is tangent to the slope.

1 Like

Thanks for the fast answer! But sorry I don’t understand the last part. Do you mean like this, if directional force is the players Input?

Vector3.ProjectOnPlane(directional force, hit.normal);

More or less, yes. There are multiple ways to approach the problem, but that should work.

I really can’t get that to work. It works but I don not get anything from it, or maybe I’m doing it wrong. What I need is something that I can normalize. Where straight up a slope is 1 and less then horizontal movement is 0.

EDIT: Got it working after some testing! Kind of. Just need to get a normalized value independent of ground angle.

super old, but i see it didn’t get resolved. i just projected the normal in the forward direction along its own plane in relation to my character:

Vector3 normal = groundedHitInfo.normal;

//set the y value to zero
Vector3 normalProj = new Vector3(normal.x, 0, normal.z).normalized;
//if this is not in the same direction as my players forward the sum will be less than 1, if so swap the direction
if((normalProj + self.transform.forward).magnitude < 1 )
{
normalProj *= -1;
}

groundAngle = Vector3.Angle(normal, normalProj);

1 Like