- Home /
Getting a GameObjects position in relation to a Terrains Resolution
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
What Im attempting to do
tData.SetHeights(X Position of Cube,Z Position of Cube, heights);
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.)
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?
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
$$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.
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
Follow this Question
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