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.

2 Answers

2

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?

Am I? hahaha~

I guess you could use [Resources.LoadAll][1], take the resulting array and loop through it. var prefabs = Resources.Load ("Prefabs/"); foreach (Object o in prefabs) Instantiate ((GameObject) o, GameObject.Find ("Canvas").transform); [1]: https://docs.unity3d.com/ScriptReference/Resources.LoadAll.html

I kind of get the feeling that not the prefab is empty but rather just doesn't show up on screen as you'd expect it would. Every UI component that you usually create in the scene under a canvas, also needs to be a child of a canvas. To do that with your instance, you call: instantiatedGameObject.transform.SetParent(canvas.transform, false); This will add it to the canvas into it's coordinatesystem, which is usually in pixels while the world is in units and (usually) times 100 (pixels per unit) larger than the canvas.

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.