Hello, I’m building a game with enemy AI and i’m using this type of code for enemies:
//target is character
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
but when the enemy steps into uneven terrain, it goes through it and then loses balance (rotation)… Can you please help?
One solution is to update the position of the enemy constantly to put it just a tiny little bit above the terrain…
Vector3 pos = transform.position;
float x = transform.localScale.y/4;
transform.position = new Vector3(pos.x,Terrain.activeTerrain.SampleHeight(pos)+(x),pos.z);
put this code in the update function or where you update the position of the transform.
it is not the optimal solution, I know that, but at least (for me) it gets the job done.
(Experiment a little bit what is the best value for x)