How to make an altometer

Hey guys,

I am making an aircraft game, and i need to make an altometer…

Now i know i could do something simple like this:

GUI.Label(Rect(10,10,200,20), "Altitude: "+transform.position.y);

but, my terrain is higher then 0 on the y access, so when my aircraft is hitting the ground, the code above says that it is at approximately 200, because thats what the y axis is…

is there a way, (maybe with raycasting), that i can find the distance between the object, and the piece of terrain directly underneath it, (i.e. if there was a hill, then the alititude value would become smaller, as there is less distance between the aircraft and the nearest piece of terrain directly below…)>

Or if there is a better way to make an altometer, then I would be happy trying something else…

Thanks

-Grady

I think first you can not use the object’s position, but the maximum and minimum height, making the bar move in proportion to the movement of the player, so this link may help if you adapt it to the Y axis

http://www.unity3dstudent.com/2011/02/platformer-progress-bar/

I hope I helped :smiley:

Why not simply move your terrain down in the scene view by 200 units in y?

You should indeed use raycasting. Instead of the normal Physics.Raycast you should use RaycastAll, to make sure you cast a ray downwards trough every object in it’s path (if there are for instance houses on your terrain). After casting the ray you check the RaycastHit against the ground’s collider, and take the delta height.

var hits : RaycastHit[];
var ground : Collider;
var height : float;
function Update () {
    hits = Physics.RaycastAll (transform.position, -transform.up, 1000.0);
    for( var hit : RaycastHit in hits ) {
       if(  hit.collider == ground )
           height = transform.position.y - hit.point.y;   
    }
}

Sorry for bumping this old post but this took me hours so here is the solution:

var hits : RaycastHit;

var height : float;

//Debug.DrawRay (transform.position, Vector3.down * 10, Color.green);//to see that the ray is always pointing down

hits=Physics.RaycastAll (transform.position, Vector3.down , Mathf.Infinity);
{
	for( var hit : RaycastHit in hits ) 
	{
		if( hit.collider.name == "Terrain" )
		{
			//height = transform.position.y - hit.point.y;//seems to work aswell
			height = hit.distance;
			print("The variable is :"+height); //debug
		}

	}
}