- Home /
terrainData.GetSteepness() usage
How is GetSteepness()
used? I just noticed, it accepts an x and y, not an x and z. This is confusing me. What z is used if x and y are given.
Also, I've normalized the coordinates based on terrainData.size
. For some reason, it returns 1 as the steepness every time, even on sloped hills.
No help for you, but I also got a similar Problem, I always get 0 as result...
//Edit: Never$$anonymous$$d... forgot to normalize it...
I dont know what you mean by you used terrainData.size, but I normalized the Vector3 of a Raycast that I had from the terrain, since I dont know how your game works I cant tell you if it would be also a solution for you.
Answer by Eric5h5 · May 20, 2013 at 12:15 AM
A terrain uses 2D data (namely, a heightmap); there is no Z used in the creation of a terrain. It's not relevant. Perhaps it would make more sense to specify X and Z, but in this case X and Y refer to the width and length.
Answer by ArtOfWarfare · Dec 13, 2015 at 03:55 AM
When using GetSteepness()
, you should provide your X and Z values in place of X and Y, and you should normalize them so that they're between 0 and 1. If your X and Z values have the same origin as the terrain but need to be normalized, you can use the following:
float normalizedX = myX / terrainData.size.x;
float normalizedY = myZ / terrainData.size.Z;
Now this part isn't documented anywhere, so this is why I really feel compelled to answer this question: GetSteepness()
returns a float between 0.0 and 90.0 representing the Euler angle of the slope. This seemed like a bizarre choice to me - I had expected it to be a float between 0.0 and 1.0.
A value between 0 and 1 would actually be bizarre. What would it represent? Still an angle? If it's an angle you would use some kind of angle unit. So either degree (what they did) or radians. If you don't want to get back an angle but the actual slope, the returned value would represent the tangent of the angle and therefore would be in range 0.0 to +inf. A value of 1.0 would be 45°, a value of 2 would be 63.435° and "+inf" would be 90°.
You never represent an actual angle as a value between 0 and 1. That wouldn't make much sense.
ps: It's actually an ordinary angle and has no relation to Euler angles. Eulerangles describe an orientation within some kind of Euclidean space
That is interesting but it does not explain his main question: Why is the slope remaining constant? If is is always the same number that means GetSteepness() is not working. Mine returns a value of 0 constantly. Even when my character walks uphill.
I can't replicate that that issue. Just thrown in a terrain, painted some hills on it and when I walk across the terrain I sample my player position. I get, as expected, the angle between the surface normal and the world up vector. Maybe you have the wrong coordinates? Have you checked you used the right coordinates? It's actually more than just dividing by the terrain size. The coordinates need to be relative to the terrain object. Of course this is not necessary if the terrain is at position (0,0,0). However you should always think about the general case.
This works perfectly for me:
Vector3 v = transform.position - terrain.transform.position;
v.x /= terrain.terrainData.size.x;
v.z /= terrain.terrainData.size.z;
var sample = terrain.terrainData.GetSteepness(v.x,v.z);
If you have multiple terrains in your world, you can additionally raycast downwards to get the objects under your feets. If it's a TerrainCollider, you can use the above code to get the angle at this point using the data from the collider you've hit.
Note that a Terrain can not be rotated or scaled with the Transform. So be careful when using InverseTransformPoint. This method does take scale and rotation into account which is not what you want since the Terrain seems to ignore those. Only the position is relevant.
So if you have issues I recommend you check the coordinates you pass into the GetSteepness function. As mentioned in the docs and above, they need to be in the range of 0 to 1.
Your answer
Follow this Question
Related Questions
Why do smaller terrains have higher triangle count? 0 Answers
terrain の detail について (About Terrain Detail),terrain の detail について (About Terrain Detail) 0 Answers
slope not working ? 0 Answers
terrainData.heightmapTexture float value range 2 Answers
Convert terrain map coordinates (GetDetailLayer) into world position 0 Answers