Reputation: 67
I have a fps camera and a model of a gun that follows her, and I have a terrain, the camera is moving on the terrain just fine, but I have a problem, I want to stop the movement if the camera is moving to high place (if it tries to move to a cliff or another high place I want to stop this option of moving to very high places) I dont mean preventing to move on high places I mean only when there is very high slope, hope you will understand and will be able to help!
Upvotes: 0
Views: 360
Reputation: 6547
If you are able to get information from the terrain where you are walking on, it is also possible to get information on the angle of the terrain.
The terrain exists out of different triangles, since it is a mesh. Every triangle has 3 vertices, but also has a so called: normal.
The normal of the face, is the direction that is pointing upwards. With simple angle calculations, you can check if the angle is too steep or not.
// in pseudo code:
public bool TooSteep(Vector3 position, float maxAngle)
{
// get your information from the terrain
// there is probably some function, or you have to write it,
// that returns the normal from the terrain
Vector3 normal = myTerrain.GetNormal(position);
// then we calculate the angle between the 'up'-vector and our normal vector
if (Vector3.Angle(normal, Vector3.up) > maxAngle)
return true;
else return false;
}
So suppose that our max angle is 45 degrees and we have a very steep normal. The angle between the up-vector and the normal will be large. Larger than our maxAngle
and will therefore return: yes, it's too steep.
Upvotes: 3