Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Grady · Jul 08, 2011 at 09:55 AM · axisheightyaircraft

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joshua · Jul 08, 2011 at 10:47 AM 0
Share

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. ;)

avatar image Grady · Jul 08, 2011 at 11:11 AM 0
Share

yeah, Graham's got the idea.... I'll edit the question to make it clearer.... sorry 'bout that....

avatar image Joshua · Jul 08, 2011 at 11:14 AM 0
Share

So your question was basically how do I find the heightdifference if one of them is not at zero? $$anonymous$$yHeight-OtherHeight=deltaHeight. facepalm

avatar image
0

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;   
     }
 }
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Grady · Jul 08, 2011 at 11:18 AM 0
Share

the height variable in that displays the exact same as just simply going:

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

avatar image Joshua · Jul 08, 2011 at 11:20 AM 0
Share

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.

avatar image Grady · Jul 08, 2011 at 11:47 AM 0
Share

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....

avatar image Joshua · Jul 08, 2011 at 01:29 PM 0
Share

And you are checking against the hit.point.y? Not the hit.collider.transform.position.y?

avatar image
0

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
         }

     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges