- Home /
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
Answer by Unamine · Jul 08, 2011 at 10:08 AM
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 :D
Answer by Graham-Dunnett · Jul 08, 2011 at 10:24 AM
Why not simply move your terrain down in the scene view by 200 units in y?
I think he wants to know the current height difference between where the airplane is, and where he would crash on the ground. So if there is a hill he wants it to lower the relative altitude. But the question is not phrased very clearly. ;)
yeah, Graham's got the idea.... I'll edit the question to make it clearer.... sorry 'bout that....
So your question was basically how do I find the heightdifference if one of them is not at zero? $$anonymous$$yHeight-OtherHeight=deltaHeight. facepalm
Answer by Joshua · Jul 08, 2011 at 10:32 AM
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;
}
}
the height variable in that displays the exact same as just simply going:
GUI.Label(Rect(10,10,200,20), "Altitude: "+transform.position.y);
Only if you have a flat terrain. This will take hills into account. It will be the exact heightdifference between your current position and crashing.
my terrain is slightly uneven, i have used the terrain toolkit:
http://unity3d.com/support/resources/unity-extensions/terrain-toolkit
And added noise to the terrain, so it is slightly uneven, but it outputs the same values....
And you are checking against the hit.point.y? Not the hit.collider.transform.position.y?
Answer by pietersmobile · Jun 10, 2014 at 09:41 PM
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
}
}
}
Your answer
Follow this Question
Related Questions
shooting y axis 1 Answer
Inverse the mouse position value 2 Answers
Sliding cube around on x/z axis with a constant y-axis 0 Answers
How to Increase Character Controller Height on Z-Axis 1 Answer
Drag object with touch on y axis 0 Answers