Enemy Ai Not Working As Intended

Godot Version
4.1.2

I am currently having an issue with my game that I am creating. More specifically the enemy ai. Whenever I approach the enemy with my player, the enemy moves away in a diagonal line to the left. Here is my code for the enemy

extends CharacterBody2D

@export var speed = 25
var player_chase = false
var player = null

func _physics_process(_delta):
if player_chase:
position += (player.position - position) / speed

func _on_area_2d_body_entered(body):
player = body
player_chase = true

func _on_area_2d_body_exited(_body):
player = null
player_chase = false

The game is a top down horror/fighting game. The enemy ai has a characterbody2d with a collisionshape2d, sprite2d, and an area2d. The Area2d has a child node to it which is a collisionshape2d. The enemy is a white square (hence the sprite2d) with a traditional collision and the aread2d’s collisionshape is a circle that surrounds the enemy. Whenever the player enters the area2d the enemy is supposed to give chase, but as I said earlier the enemy just moves in a diagonal line away from the player wherever I approach it. The code I used is from a youtube video called, " Godot 2D How To Add Enemy AI" by
Benjamin Game Des. Any help will be greatly appreciated.

did you add player as a scene?

At least if the code you shared is complete, the enemy will only move when the player is inside its Area2D. If it’s not moving it in the correct direction, that likely means the player.position and the enemy position are relative to different origins points (depending on where exactly they are located in the tree). Try this instead:

position += (player.global_position - global_position) / speed

If it still doesn’t work then, please share a screenshot of your scene tree.

Yes, my player is a scene by itself

The problem is still happening with my enemy. Here’s the scene tree of the enemy:
image

check if all the child are on top of the character2D node for both the character and the enemy

They are all under the character2D node

sorry for the misleading wording
what I meant was to check the transform.position of all the children in the enemy and player scene

Sometimes people might drag the sprite and the collision out to the middle of the screen making it easier to edit and that might cause some problems while calculating the position

I should also mention that I am new to coding so I won’t be able to fully understand everything. Just know that I am taking steps into understanding what there is about Godot. But to answer you I left the sprite in the middle of the screen, but the player position itself in the cross section

yes, this might be the reason if you instantiate the scene you should reset the collision and the sprite back to 0.0

Maybe it’s happening because your player variable is null when it’s entering area2d (just guess)

How about to try this instead?

Make a group called “player” here
image

Then add your player here and write this in code

@export var speed = 25
var player_chase = false
var player = get_tree().get_first_node_in_group("player")

func _physics_process(_delta):
   if player_chase:
      position += (player.global_position - global_position) / speed

func _on_area_2d_body_entered(body):
   player_chase = true

func _on_area_2d_body_exited(body):
   player_chase = false

Also, if you’re sharing your code, please write “~~~” at the start and the end of your code, so if will be easier to understand. And, if it’s possible, may you send screenshoot of your players and enemy’s scene if it won’t work?

It worked. Thank you so much.

1 Like

I got it working. Thanks for the tip though.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.