- Home /
Moving along surface without physics?
If I want to keep my game light without hundreds of objects using physics, how would I make my enemies move along the terrain if it's not 100% flat? I'm looking for some way to just set "enemy.position.y = terrain height below the enemy" or anything as simple.
Answer by Eric5h5 · Dec 05, 2010 at 07:26 PM
If it's an actual Unity terrain, use Terrain.SampleHeight().
Could you give some example how to use it? transform.position.y = Terrain.activeTerrain.SampleHeight(transform.position); Didn't work
height = Terrain.activeTerrain.SampleHeight(transform.position); transform.position = new Vector3(transform.position.x, height, transform.position.z); Seems to work but my enemy rotation gets messed up and howers like 20 meters on ground. Strange..
Still can't get this working. "Cannot movide a value type return value of 'UnityEngine.Transform.position' Consider storing the value in temporary variable"? Both the enemy transform and height from SampleHeight are in temporary variables. What I'm still supposed to do?
Use Javascript ins$$anonymous$$d of C#, in which case you aren't required to manually store values in temporary variables when you just want to modify transform.position.y. ;) Other than that I couldn't say without knowing what you're doing.
Apparently it works now with C# but the height given with SampleHeight is incorrect for some reason. $$anonymous$$y enemy is at 197 on y axis, the terrain is 2 units lower at 195 and after setting my enemy's y axis to terrain height gotten from SampleHeight the result is 217. I'd like to have some example. No idea what I'm doing wrong.
Your answer
Follow this Question
Related Questions
Too many texture interpolators would be used for ForwardBase pass (again) 1 Answer
CG diffuse shader 1 Answer
Moving a point along a plane, mesh, or collider 1 Answer
Make a simple tree 1 Answer
Walls not moving in 1 Answer