- Home /
Making enemy follow character on uneven terrain
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?
Answer by softrare · Sep 03, 2012 at 12:53 PM
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)
Did this help you, or does the problem continue to exist? Please give some kind of feedback.
Thanks!! sorry i couldnt find my own post.. it worked perfectly!!
Your answer
Follow this Question
Related Questions
My enemy is not moving properly on a terrain 0 Answers
Complex Enemy follow AI 1 Answer
Enemy Follow Player Without Rigidbody2D 2 Answers
How do I make a enemy follow me 3 Answers
How can I make a flying object follow ground player and stay airborne? 2 Answers