- Home /
Causing enemies to slide around walls
Hi, I'm making a tower defense game where you can build walls. I would like it if enemies collided with a wall they would then try to slide down the wall to get around it. Unfortunately, I haven't been having any luck or many ideas. I'm using
rigid.MovePosition(Vector3.MoveTowards(transform.position, SelectedTarget.position, speed * Time.deltaTime));
for the movement. I originally had the idea that I could make it so when the character hits a wall, they turn to the side and walk forwards until they aren't touching the wall anymore, but that lasted about until I realized it only worked with specific wall angles and not in general (here was my code for that):
void Update()
{
if (noWall)
rigid.MovePosition(Vector3.MoveTowards(transform.position, SelectedTarget.position, speed * Time.deltaTime));
if (!noWall)
{
rigid.MovePosition(transform.position + transform.up * Time.deltaTime * speed);
}
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Wall")
{
Quaternion deltaRotation = Quaternion.Euler(new Vector3(0, 0, 90));
rigid.MoveRotation(transform.rotation * deltaRotation);
noWall = false;
}
}
So I guess I have two questions. Is there a simple way for me to evolve this code into something that will work no matter the wall rotation (keeping in mind that the enemy is not approaching from the same angle all the time), and if not, does anyone have a suggestion on how to accomplish this (code example appreciated but unnecessary)?
Answer by TanselAltinel · Oct 24, 2017 at 09:25 AM
Why not use navigation mesh so that your agents will avoid obstacles like walls?
Your answer
Follow this Question
Related Questions
Unity Mechanim playing default layer state animation when gameObject is duplicated in the scene 0 Answers
Enemy follow player when player is not looking. OnBecameInvisible not working. 1 Answer
Run coroutine only when player stays inside collider? 0 Answers
How to issue order to enemies 1 Answer
How to validate camera when collides with a wall - ThirdPerson 2 Answers