• 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 Kronnas · Jul 20, 2014 at 09:12 AM · terrainterraindata

SampleHeight vs GetHeight?

Why terrain.SampleHeight and terrain.terrainData.GetHeight returning diferent results? Hi i work with terrain (placing Object on it) and when i work with height of terrain a discover that these two method returnig diferent result (and i cant find out why).

  float height = terrain.SampleHeight(new Vector3((int)position.x, 0, (int)position.y));
  float height2 = terrain.terrainData.GetHeight((int)position.x,(int)position.y);
  Debug.Log(height.ToString() + ":" + height2.ToString());
 

(retype to int just for purpose of test) and result are guite interesting Sometimes is result almost same like (89.60941:85.10804), but i found few position which are totaly diferent like (0:80.23256) It seems that Sampleheight is more acurate, but why?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by tanoshimi · Jul 20, 2014 at 09:17 AM

terrainData.GetHeight expects an int x and y coordinate expressed relative to the size of the terrain. So, if your terrain is 2048x2048, valid x and y would range from 0-2048, and it returns the height of the terrain from the heightmap at that point.

terrain.SampleHeight expects a Vector3 position in worldspace, and returns the interpolated height of the terrain at that point.

So they measure totally different things - if you were getting similar results from a given input that's coincidence.

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 Kronnas · Jul 20, 2014 at 09:24 AM 0
Share

But when i place Terrain to 0,0,0, that SampleHeight(new Vector(){10,...,10} and getheight(10,10) should return same Value or not? If of course terrain is big enough (more than 10x10) Or that interpolated value is that much diference?

avatar image tanoshimi · Jul 20, 2014 at 09:40 AM 0
Share

From memory, I think terrain heightmaps have (0,0) at the top left corner not the bottom left. So I would expect SampleHeight(new Vector(){10,...,10} and GetHeight(Terrain.height-10,10) to be (approximately) the same.

avatar image Kronnas · Jul 20, 2014 at 09:46 AM 0
Share

Ok thanks, i will test it and post result $$anonymous$$aybe last question: GetHeight is faster than SampleHeight? (reagin from heightmap - picture vs calculating some positions)

avatar image tanoshimi · Jul 20, 2014 at 09:51 AM 0
Share

Yes, GetHeight should be faster, because it's basically just a lookup into an array.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Object2Terrain Creates a Invalid Collider. Help please. 1 Answer

Terrain and PerlinNoise Glitch 0 Answers

Why do smaller terrains have higher triangle count? 0 Answers

terrain の detail について (About Terrain Detail),terrain の detail について (About Terrain Detail) 0 Answers

Bake terrain normal via script 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