I'm trying to enhance a simple clamp to terrain script I wrote to also rotate the object based on the angle of the terrain under it. There is probably easier ways to do what I am trying to do, but I'd also like to know why my code is simply not working.
I feel like I'm pretty close, but the angles are way too small (< 0.3), and assigning the new rotation Quaternion to the transform object seems to do nothing. Even hard coding larger angles resulted in no change.
asked Aug 17 '10 at 07:31 PM
The Slerp interpolation will only work if this function is called repeatedly (i.e., in Update()), since this variant of using a Lerp function (by giving it Time.deltaTime as interpolation parameter) is more like a "smooth follow", no longer a linear inerpolation. You might also want to increase the 2.0 scaling, to influence the speed at which the new rotaion angle is adapted.
Note that there is a method called Terrain.terrainData.GetInterpolatedNormal(x,y) which gives you the normal at a given position. However, since it is a method of the heightmap data, the values x and y are normalized between 0 and 1, so you would have to compute them by yourself, using your pos.x and pos.z and the actual terrain dimensions+location.
answered Aug 17 '10 at 09:35 PM
I know this is old, but it may help others as it helped me:
answered Jun 22 '11 at 03:21 PM