- Home /
Enemy AI for top down shooter
I have a top-down shooter where the player can move freely in the screen. I want to add enemy AI in a way that when the enemy is off the screen, he needs to follow the player and once he's inside, movement should be randomised. I know I could use Vector3.MoveTowards() to get the enemy follow the player, but how do I know if he's inside or outside the camera? I tried using OnBecameInvisible() and OnBecameVisible() triggers but the enemy keeps moving away from the player. What am I missing here or is there any other approach to solve this?
Answer by shipgamescompany · Jun 18, 2019 at 01:22 AM
Just a suggestion, but an alternative is to create a radius using colliders.
So you could code something like when the enemy enters the 2Dcollider he has random movement, and when he leaves he moves towards the player.
See these links for reference:
Answer by Roy9720 · Jun 18, 2019 at 11:59 AM
I used the Camera.main.WorldToViewportPoint() and it worked, thanks for the answer.
Your answer
Follow this Question
Related Questions
where to trigger which collision event? 1 Answer
8-directional orientation, top down 2D, seperate from movement 0 Answers
Top Down shooter Moving while Shooting 0 Answers
Waypoint navigation in 2D without LookAt 2 Answers
2D Top-Down 3D-Like effect 2 Answers