Search Unity

A better way to go up slopes / test for ground normal?

Discussion in 'Physics' started by dgoyette, Dec 23, 2019.

  1. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    In my rigidbody character controller, I have some extra code for helping the player go up an incline. My original controller didn't have this, and the result was that going up even minor inclines was impossible, as the physics force directed horizontally into the slope would generate tons of friction, and the player would just stop. So, my current controller tests the normal vector of the ground where the player is standing, and changes the direction of their movement force to be parallel to the ground. This works very well for going up ramps, but it introduces a pretty annoying problem: False positives.

    The bad thing that happens is if the player approaches an irregular shape, like a random chunk of rock, the ground test (just a raycast) has a tendency to hit an inclined face on the object, making my controller think the player is on a steep surface. In reality, they're on a flat surface, but there's a rock under them. The result of this is that it's now really hard to move horizontally, as the movement force is no longer horizontal.

    I took some advice a while back, and tried to make the ground check more accurate by adding two more raycasts, and aggregating the normals. But this doesn't really help much. The player can still step up onto a small piece of rock, and two of the raycasts can hit a steep face of the rock, and now the player can't move. Here's a bad drawing of what often happens:

    upload_2019-12-23_18-19-55.png

    The red object is some random rock with a relatively streep face. The green lines are the three raycasts I'm using to test the ground normal. (one under the player, one ahead, and one behind). The result is that as the player just barely step up onto the rock, there are now two raycasts getting high angles, so the player thinks he's on a very steep slope, and can't even walk backwards because the force direction goes into the floor.

    Is there some other general approach I can be taking to testing for ground normals, that don't get tripped up by these kinds of things?

    I should mention that one reason the player gets "stuck" on this, and can't move forward or backward, is an extra bit of logic I have, where if the player is trying to go up a slope that's too steep, I reduce their movement force to 0, just to make sure they definitely can't go up a very steep ramp even if the physics would have allowed it. So, now the player bumps into this rock, and the controller thinks he's on a 60' ramp, and reduces all movement force to 0, so he can't even walk backwards anymore. I suppose I could tweak that, and maybe only diminish the force if they're trying to go "up" the ramp, but not constrain it when moving "down" the ramp.
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,195
    Okay, the last paragraph of my post above made me realize that most of the problem was that I was messing with the character's backward movement speed while they were facing uphill. I guess I did that so that downhill movement would be smooth, and follow the surface, rather than feel like you're hopping down the hill. But that was really the issue. As long as I don't constrain their downhill movement, everything fine, and I don't get stuck on small rocks anymore.

    So I guess it wasn't the normal detection technique, it was a mistake in how I was using the information. I'm all set here.