- Home /
Astar Pathfinding with unity 2d
I am using Astar pathfinding in my 2d game but there is a problem.When an enemy follows the player and shoots after stopping at a particular distance from the player it doesn't ee whether there is an object between between them and shoots directly at the obstacle.I want the enemy to move around the obstacle and shoot when there is no object between them. I know we can use Raycast but I don't know how to implement it....
Answer by Llama_w_2Ls · Jan 05, 2021 at 02:13 PM
You can have a bool called PlayerIsVisible
, and if the player isn't visible, you shouldn't fire at the player. To check if the player is visible, you should fire a raycast in the direction of where the player is (`player.transform.position - enemy.transform.position` = direction) and if the raycastHit is equal to the player, the enemy can see the player without any obstacles blocking it, and so PlayerIsVisible equals true, else, it is false, and so you shouldn't shoot, instead, go around the object and shoot again when the player is visible. @revanthpershad
Your answer
Follow this Question
Related Questions
How to make enemies take wider routes? 1 Answer
A* - Enemy Pathfinding away from Player 0 Answers
A* pathfinder - next target 1 Answer
Smooth out a line renderer 1 Answer
A* (AStar) Pathfinding Issue - Path not spawning instantly 0 Answers