Search Unity

How to find object's height above terrain mesh?

Discussion in 'Scripting' started by bigkahuna, Sep 7, 2006.

  1. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    I have a vehicle that is floating over a terrain mesh and I want to find it's "altitude". I'm able to find it's height in world coordinates with this:

    Code (csharp):
    1. var Altitude = transform.position.y;
    But I want to find the actual distance along the y axis between my vehicle and the terrain mesh. I studied the docs and found that what I probably need to use are raycast, raycasthit or raycastcollider, but the docs are pretty thin and there aren't any code samples.

    Anyone have a simple code snippet to get me started? I'm using Javascript BTW. Thanks!
     
  2. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Add this to your Vehicle script.

    Code (csharp):
    1. var hit : RaycastHit;
    2. var heightAboveGround = 0;
    3.  
    4. if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.down) , hit, Mathf.Infinity))
    5. {
    6.     heightAboveGround = hit.distance;
    7. }
    8.  
     
    kev000 likes this.
  3. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Thanks, I had just figured it out and was about to post the solution!
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Code (csharp):
    1.  
    2. var heightAboveGround = 0;
    3.  
    Actually that should be 0.0, or specify ": float". Otherwise you get integer values. Just to be nitpicky. ;)

    --Eric
     
  5. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    That leads to a question I've had about Javascript but never asked.

    I've seen other folk declare their variables within Functions or IF loops, and I've now picked up the habit. For example here's my code:

    Code (csharp):
    1. var hit : RaycastHit;
    2.  
    3. if (Physics.Raycast (transform.localPosition, transform.TransformDirection (Vector3.down), hit))
    4. {
    5.     var heightAboveGround = hit.distance;
    6. }
    Is this in bad form? I used to declare all my variables at the top of a script, but at times this can be much shorter (and in this case I don't need to be able to "edit" the value of my variable later).
     
  6. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    i'd like to hear smarter coders than me comment. i'd say yes and no to the bad form q. depends. does it work? well ok good. basically in my laymens terms you're making it private vs public. the var will only be valid in the if. so if you need it elsewhere you'd declare it outside the if. but if it's a one shot and it works, who cares? that's my take.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Well, no, it's valid for that entire function, not just the "if". But if you need it for more than just that function, yeah, it should be declared outside.

    The only thing (AFAIK) "bad form" about...

    Code (csharp):
    1.  
    2. var heightAboveGround = hit.distance;
    3.  
    ...is that the type isn't declared. So it has to figure out of it's a float, integer, whatever. It obviously works fine, but it's faster to use:

    Code (csharp):
    1.  
    2. var heightAboveGround : float = hit.distance;
    3.  
    --Eric
     
  8. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    Vars are only visible within the block they are declared in, so yes it is only visible inside the if-block.


    The previous example does not result in slower code. The Unity Javascript compiler automatically infers that heightAboveGround must be a float due to the assignment. (It already knows that hit.distance is a float)

    The only place where explicitly declaring the type of a variable makes a difference is where the compiler lacks context to infer the correct type, such as this example:
    Code (csharp):
    1.  
    2. // Slow -- no context to show what type 'thing' should be
    3. function doSomething( thing ) {
    4.    thing.position.x += 0.1;
    5. }
    6.  
    7. // Faster -- type explicit
    8. function doSomething( thing : Transform ) {
    9.    thing.position.x += 0.1;
    10. }
    11.  
    My advice on "good" vs. "bad" form is only to explicitly define types if it's not obvious at the place of the declaration what type it will be. (and then only where performance really matters -- it's actually not that slow for most cases.)

    If you want to declare all types, you could just as well use C#. :)
     
  9. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,434
    Thanks very much for your help everyone!
     
  10. ranaweera

    ranaweera

    Joined:
    Dec 15, 2015
    Posts:
    15
    please let me know an objects y position from the plane(I use a plane instead of a terrain)-c sharp
     
  11. Yash987654321

    Yash987654321

    Joined:
    Oct 22, 2014
    Posts:
    729
    See how old this was -_-
     
  12. Harald_Heide

    Harald_Heide

    Joined:
    Jul 22, 2015
    Posts:
    81
    transform.position.y = Terrain.activeTerrain.SampleHeight(transform.position);

    Age doesn't matter :)
     
    SkylerLewis likes this.