Does anyone have a script or a method (other than placing collision) that will make it so that a character controller can not shimmy (via jumping again and again) up a steep terrain?
I imagine this is a very common problem; there should be default solution included in Unity, the terrain, or the Character Controller.
1 Like
Why not just have your movement script get the surface normal of the surface you are on (like with TerrainData.GetInterpolatedNormal) and if it is more than a certain threshold away from straight vertical, prevent the jump or modify it in some way that makes sense for the context of your game?
For the player, it only has to be checked once per “jump” press, so that isn’t too expensive. For AI, where that solution might be wasteful, just don’t include those areas in your navmesh or other pathfinding implementation.
That’s a good suggestion Charles, but it really revealed that I posed the wrong problem. I’d almost never want to take away the ability for a player to jump.
Perhaps the real problem is that when the terrain is steep, the player doesn’t slide down it. It is the player’s ability to stay in place that allows them to make progress with sequential jumps.
1 Like
There are two approaches I use to prevent players from jumping up steep hills. One is to apply sliding force:
//calculate a vector that runs across the slope
var tangent : Vector3 = Vector3.Cross(normal, Vector3.up);
//from that, calculate the direction of steepest descent
var down : Vector3 = Vector3.Cross(normal, tangent);
Then you can apply movement or force or whatever in the direction of down, and you’ll get sliding.
The other approach is simply to have the player’s jump affected by the normal of the slope they are on if it is too steep. This way they will jump away from the hill if they are on it, and even though they can get a foothold, jumping will just take them further downhill.
Of course, the two methods can be used together for completely unclimbable slopes.
2 Likes