altitude checking from ground

I’m working on a small game, I’ve everything done, its an helicopter game and I don’t want the helicopter to fly higher than a certain amount of units from the ground (an irregular terrain) To do so, I want the helicopter to ray cast a ray that ignores all layers except the “ground” one and if the ray is larger than a certain amount of units I want the player to be unable to fly higher than that, the up movement is controlled with the space bar and its a force movement.

First set your irregular terrain to a layer “Terrain” (no other objects in this layer. Let’s say the layer id is 1. (last parameter of Raycast function)

var height_limit:float=3000;

function GetHeliHeight:float(){
 var hit : RaycastHit;
 var heli_pos:float=GameObject.Find("Helicopter").transform.position;
 hit = Physics.Raycast (heli_pos, Vector(0,-1,0),1);
 return (Vector3.Distance(heli_pos, hit.collider.transform.position));
}

function HeliControl(){
 if(GetHeliHeight>=height_limit)
 //can't move up
 else
 //can move up
}

Now you know roughly how to implement?

That’s more of a statement than a question :slight_smile:

I’m assuming though that the question is how to go about restricting the helicopter’s altitude - is that correct? If so, here’s one possible solution: when the helicopter is ‘over altitude’, apply a downward force proportional (with the proportion determined by a constant coefficient) to the difference between the current altitude and the maximum desired altitude.

Note that this will allow the helicopter to go a bit over altitude (how far will be determined by the constant force multiplier), so if you need a hard limit, you might need to use another method.