• 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 /
  • Help Room /
avatar image
1
Question by Phuc Vo · Oct 23, 2015 at 05:31 AM · heightmountains

Find height of mountain in terain?

I'm working at Unity 5.0, and now I random Items above my terrain. But I have a problem, I cannot get Height of mountain in terrain.

 pos.y = Terrain.activeTerrain.SampleHeight(transform.position) + 5;

this code just get constant height of terrain. And my Item cannot appear on my terrain where have mountain! and somewhere it have haft inside mountain!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Statement · Oct 23, 2015 at 03:15 PM

SampleHeight samples the height of the terrain data, but it does not take into account offset if you have the terrain at any Y than 0. You have to account for this yourself.

It may be that your Terrain has a parent game object that is offset.

Make sure the terrain and anyof its parents are not rotated or scaled.

Then this code should work:

 using UnityEngine;
 
 public class FollowTerrainContour : MonoBehaviour
 {
     void Update()
     {
         float terrainSampleWorld = GetHeight(transform.position);
         UpdateY(terrainSampleWorld);
     }
 
     void UpdateY(float terrainSampleWorld)
     {
         Vector3 pos = transform.position;
         pos.y = terrainSampleWorld;
         transform.position = pos;
     }
 
     float GetHeight(Vector3 position)
     {
         float terrainWorldY = Terrain.activeTerrain.transform.position.y;
         float terrainSampleLocal = Terrain.activeTerrain.SampleHeight(position);
         float terrainSampleWorld = terrainWorldY + terrainSampleLocal;
         return terrainSampleWorld;
     }
 }

Or less verbose:

 height = Terrain.activeTerrain.SampleHeight(position) 
        + Terrain.activeTerrain.transform.position.y;
Comment
Add comment · Show 1 · 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 Statement · Oct 23, 2015 at 03:18 PM 0
Share

Also it appears I have answered this before.

avatar image
0

Answer by PictonicStudio · Oct 23, 2015 at 06:55 AM

Have you tried casting a ray down towards the position you want to query? You could then find the height by using Vector3.DIstance.

 Vector3.Distance(hit.transform.position,new Vector3(hit.transform.positon.x,seaLevel,hit.transform.position.z));


hit being the RayCastHit variable, and seaLevel being the base height you want to measure the height from.

Comment
Add comment · Show 1 · 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 Phuc Vo · Oct 23, 2015 at 08:24 AM 0
Share

Sorry, can you tell me more detail about that. I'm create new hit object and I think seaLevel is $$anonymous$$ of terrain height. But my Code is not work and I don't know how hit work.

 RaycastHit hit = new RaycastHit();
 pos.y = Terrain.activeTerrain.SampleHeight(transform.position) + 5;
 Vector3.Distance(hit.transform.position, new Vector3(pos.x, pos.y, hit.transform.position.z));

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

32 People are following this question.

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

Related Questions

Script attached to different objects always return same terrain height on different locations 2 Answers

How to Compare The Height oF TWo Height?? 0 Answers

Unity- Controlling Jump Height 0 Answers

Terrain smoothing brush opacity settings 0 Answers

Endless Runner platform Heigh generator not working. 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