- Home /
Enemy strafing AI Top Down
Hello. I am trying to wrap my head around a simple problem where I must have my enemy game object strafe around my player.
I am using Unity2D physics and have a game from the top down perspective.
So the enemy has to face the player as it moves around it's position (I have a rotation target script, so I just need to set the player as the rotation target in order to face them). Assume that the position the enemy is rotating around will not change as the player moves too. What I want is for it to move along the circle at a given velocity, and if the next point on the circle it needs to strafe around will cause a collision, strafe the other way. I assume this check will have to be done by circle casting my collision box (CircleCollider2D) and seeing if it makes contact with a wall.
My thinking is that I can take the distance I want to maintain and the angle of the enemy relative to the player, and the vector component method with an incremented angle to find the next point on the circle I want to go to (x = dist * cos(angle + increment), y = dist * sin(angle + increment)).
My issue is finding this increment value. If I'm incrementing in the update, I would have to make sure not to increment faster than the speed I am reaching the points, or else my target position will move faster than my enemy and cause weird behavior. I also need to make sure I have this value because I need to measure the distance from the enemy to the next point I want to go to and use a circlecast there to see if there is a collision.
I would appreciate some advice, or if someone recommends a different way to go about solving this problem I would be interested in hearing.
Your answer
Follow this Question
Related Questions
Unity2D AI for Top Down Zelda-Type movement 1 Answer
Top down wasd movement dependent on rotation of the character - Mathematical problem!!! 2 Answers
Game NPC "Guide" Movement (Much like Navi from Zelda) 1 Answer
Need Help on Obj Movement via Mouse (particular physics rulles) 1 Answer
Help with 2D AI scripting 2 Answers