Godot Version
4.3
Question
I’m making a 2d top down shooter game. I have a ranged enemy that I have created that is able to fire at the player. The issue is it will often stand next to a corner, causing its projectiles to not reach their target. Is there some way to position it to avoid this?
The current code to find it’s path is this:
func make_path():
if player != null:
sight_ray.visible = true
var dis_to_player = position.distance_to(player.position)
if sight_ray.get_collider() == player:
# get positon to stop at
var angle_to_player = position.angle_to_point(player.position)
var target_position = player.position + -distance_to_stop.rotated(angle_to_player)
nav_agent.target_position = target_position
else:
nav_agent.target_position = player.position
essentially the enemy will check if the player is in it’s line of sight using a Raycast2D. If the player is it will try and path the closest point that is a set distance away from the player. Otherwise the enemy will try to path to the player’s actual position. This leads the enemy only stopping when the player is in its line of sight.
I’m wondering if there’s anyway to modify this to keep the enemy’s firing path from being to close to walls?
Thank you for reading.