Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | 1MochaChan1 |
I am referring to this Navigation2D Tutorial .
Although I have got my enemy character to chase the Player when it is in sight but I want the Enemy AI to complete its last 2D path generated and then stop whenever my player hides behind a wall or something.
This is the code for Generating The Path for the Enemy AI to follow :
func generate_path():
if levelNav != null and player != null:
path = levelNav.get_simple_path(global_position, player.global_position, false)
line2d.points = path
This is the code for the Enemy AI to move towards the player :
func navigate():
if path.size()>0:
if path.size() == 1:
velocity = global_position.direction_to(path[0]) * speed
elif path.size() > 1:
velocity= global_position.direction_to(path[1]) * speed
if global_position == path[0]:
path.pop_front()
And this is how I call them in my physics_process(delta)
:
func _physics_process(delta):
...
player_detect()
if player_spotted:
generate_path()
navigate()
move()
- Player detect is nothing but a function to check if the raycast from the Enemy AI is colliding with the player or a wall
However whenever player_spotted == false
no more path is generated (which is what I want) but in the code where Enemy move towards the player the line of code if global_position == path[0]
never gets executed and the enemy AI keeps twitching at a point specified in the path array
This is what it looks like :
Any help is appreciated,
Thank you for your time.