Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by Ryoni · Dec 07, 2010 at 03:36 PM · terrainpositionaxisheight

Incorrect height with sampleHeight

The case is: I have cube which I want to move along a non-flat terrain just by setting the cube.position.y = the height of terrain below the cube. All this with C#.

I was adviced to use sampleHeight but after trying dozens of different forms and even javascript the height was never correct.

By the way even the "Terrain.SampleHeight" example (as C#) in Unity API gives an error BUT I managed to get it working (but with incorrect height) like this:

height = Terrain.activeTerrain.SampleHeight(cube.position);
cube.position = new Vector3(cube.position.x, height, cube.position.z);

With this piece of code, during the game if I move the cube around the terrain, it's always 22 units too high on y axis. Of course I could just add -22 to the height but why it's incorrect in the first place? Is there something wrong with the code because I had to find it out myself?

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
9
Best Answer

Answer by Statement · Dec 07, 2010 at 03:45 PM

There is nothing wrong with SampleHeight. If you read the docs, it says the value is in terrain space. The reason you're having 22 as your magic number is probably because your terrain is located 22 units away from the world origin.

Try to use this instead:

height = Terrain.activeTerrain.SampleHeight(cube.position) 
       + Terrain.activeTerrain.transform.position.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 Ryoni · Dec 07, 2010 at 03:52 PM 0
Share

Yes! That's it! Good lord I got it finally working. $$anonymous$$y terrain was apparently a child of my 'scene' -empty object so I had probably moved it sometime. Thank you!

avatar image Statement · Dec 07, 2010 at 03:54 PM 0
Share

Now carry on and make great wonders with this new technology :)

avatar image Ryoni · Dec 07, 2010 at 04:01 PM 0
Share

Will do! Hopefully.. :)

avatar image Ryoni · Dec 08, 2010 at 02:50 PM 0
Share

Interestingly I found out that both $$anonymous$$e and your example works but my code drops the fps from 215 down to 80 and yours not at all. I'd like to know what $$anonymous$$e does too much without the "+ Terrain.activeTerrain.transform.position.y; " -part. After all isn't it just getting y from x,y,z position? I don't understand how does adding additional y make it any faster.

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

2 People are following this question.

avatar image avatar image

Related Questions

Character height issue 0 Answers

Raise/Lower terrain height? - javascript. 1 Answer

How to stop at a point of axis? 1 Answer

Terrain Tree Height not working on Unity 5.3.4f1 0 Answers

How to Lerp a position along one axis only? 2 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