Xna prevent descending slopes or hills that are too steep
2 answers
I would avoid scaling / running computations as they might produce NaN if the terrain is vertical.
//currentPosition & targetPosition should be known to you
Vector3 potentialMove = Vector3.Normalize(targetPosition - currentPosition);
float steepness = Vector3.Dot(Vector3.Up, potentialMove);
if( steepness < 0.85f && steepness > -0.85f) // set to taste. 1 is vertically up, 0 is flat, -1 is vert down
{
//allow move
currentPosition = targetPosition
}
+1
source to share
You have to predict where you are going to end up if you try to move in a direction, and then figure out if the slope between your current point and your future point is too steep:
if(forward key pressed) {
get location I'll end up at
get the Z of that location
calculate slope using rise/run formula
if(slope is too steep) {
don't move
}
else { move to the future location }
}
+4
source to share