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
0
Question by MattyG08 · Sep 27, 2012 at 12:30 PM · terrainprogrammingrtssetheights

Getting a GameObjects position in relation to a Terrains Resolution

alt text

Hello, I have to use this Diagram to help explain my issue. I am essentially Setting the Height of the Terrain underneath the Cube to the height of the Cube.

The Issue is, I cannot find a way to get the position of the Cube relative to the Resolution of the Terrain. ( If I use the actual position of the cube it simply says it is out of bounds ) Im sorry if this is hard to explain, but Im hoping people may have had done things similar to this before.

All help is appreciated. thankyou

diagramm.jpg (26.2 kB)
Comment
Add comment · Show 1
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 MattyG08 · Sep 27, 2012 at 11:28 AM 0
Share

What Im attempting to do

tData.SetHeights(X Position of Cube,Z Position of Cube, heights);

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Sep 27, 2012 at 02:21 PM

Terrain has little "squares" that have the corners raised/lowered. SetHeight takes the corner #'s as input. Ex: `setHeight(1,3)` goes one corner east and 3 north, and changes that one. Since the squares aren't 1 meter x 1 meter, you have to convert world x,z into nearest corner #.

To convert, 1st get the width of each square: divide the total Terrain width by the number of squares. Then divide your world x,z by those and round to the nearest. Ex: if you are at x=22 and terrain tiles are 5 wide, you are closest to corner #4, at x=20.

Searching for "Unity Terrain Set Height" gives a result from Unity Answers where they do all that math (or try "Unity world Terrain" for another.)

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 MattyG08 · Sep 28, 2012 at 07:47 AM 0
Share

Thanks for the Help, but it hasn't been too usefull, When searching for things related to World Coordinates - Terrain Coordinates the questions given aren't really explained properly. Any chance you could provide some example code?

avatar image MattyG08 · Sep 28, 2012 at 08:38 AM 0
Share

SOLVED! I just did some Lateral thinking and I will post the simple code for anyone else to follow if they ever have the same issue.

var Relativex = transform.position.x - TerrainObject.transform.position.x; var Relativey = transform.position.z - TerrainObject.transform.position.z; Debug.Log("X: "+Relativex+" Y: "+Relativey);

var RelaY$$anonymous$$k2 = Relativey 2; var RelaX$$anonymous$$k2 = Relativex 2;

tData.SetHeights(RelaX$$anonymous$$k2, RelaY$$anonymous$$k2, heights);

However, this System needs alot of fine-tuning to relate to Resolution of the Heightmap

avatar image Owen-Reynolds · Sep 28, 2012 at 03:05 PM 0
Share

$$anonymous$$ost people leave the Terrain at (0,0) so don't need to compute your `Relativex` and `y` vars (they are the same as x and z.) But, if yours is moved, that looks perfect.

The 2nd part, where you multiply by 2. That's saying that in your case your terrain has exactly 2 squares per meter. $$anonymous$$any terrains don't line up that nicely, which is where the math comes in.

avatar image MattyG08 · Sep 29, 2012 at 12:07 AM 0
Share

Yeah I ended up having to re-write it to deal with a larger terrain.

var Relativex = transform.position.x - TerrainObject.transform.position.x; var Relativey = transform.position.z - TerrainObject.transform.position.z;

var RelaY$$anonymous$$k2 = Relativey / terrain.terrainData.size.z; var RelaX$$anonymous$$k2 = Relativex / terrain.terrainData.size.x;

var Dimx = RelaX$$anonymous$$k2 terrain.terrainData.heightmapWidth; var Dimy = RelaY$$anonymous$$k2 terrain.terrainData.heightmapHeight;

Debug.Log("X: "+Dimx+" Y: "+Dimx);

tData.SetHeights(Dimx, Dimy, heights);

This one works perfectly for me now.

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Smooth camera height adjustment based on terrain 1 Answer

please explain Terrain.SetHeights 3 Answers

Problems with Translating Java in C# 1 Answer

Create terrain with grid 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