2D RPG Trying to make an enemy chase the player.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By KennethM

I am currently following DevWorm’s tutorials on how to make an RPG but in episode 4 with making a enemy my code isn’t working. Instead of the enemy chasing the player in will move to a randow spot relative to the player for example the enemy will move 2 tiles to the left and 1 tile up from the player. If anyone knows how to fix this please let me know. Thanks.

You didn’t really provide enough information for us to give you any sort of help.
With this alone, the best I could (cinically) give is: rewrite it so it’s not moving 2 tiles to the left and 1 tile up.
We don’t know what is the expected outcome: you should have pasted a link to the video, or even better, the reference code
We don’t know how your version works: you should have pasted your own code.

zhyrin | 2023-07-07 06:15

Here is my code for the enemy:
extends CharacterBody2D

var speed = 50
var player_chase = false
var player = null

func _physics_process(_delta):
if player_chase:
position += (player.position - position)/speed
$AnimatedSprite2D.play(“Walk_Enemy”)

	if(player.position.x - position.x) < 0:
		$AnimatedSprite2D.flip_h = true
	else:
		$AnimatedSprite2D.flip_h = false
else:
	$AnimatedSprite2D.play("Idle_Enemy")
move_and_slide()

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

func _on_detection_area_body_exited(body):
player = null
player_chase = false

Here is the code for the player
extends CharacterBody2D

const speed = 75
var current_dir = “none”

func _ready():
$AnimatedSprite2D.play(“Front_Idle”)

func _physics_process(delta):
player_movement(delta)

func player_movement(delta):

if Input.is_action_pressed("move_right"):
	current_dir = "right"
	play_anim(1)
	velocity.x = speed
	velocity.y = 0
elif Input.is_action_pressed("move_left"):
	current_dir = "left"
	play_anim(1)
	velocity.x = -speed
	velocity.y = 0
elif Input.is_action_pressed("move_down"):
	current_dir = "down"
	play_anim(1)
	velocity.y = speed
	velocity.x = 0
elif Input.is_action_pressed("move_up"):
	current_dir = "up"
	play_anim(1)
	velocity.y = -speed
	velocity.x = 0
else:
	play_anim(0)
	velocity.x = 0
	velocity.y = 0

move_and_slide()

func play_anim(movement):
var dir = current_dir
var anim = $AnimatedSprite2D

if dir == "right":
	anim.flip_h = false
	if movement == 1:
		anim.play("Side_Walk")
	elif movement == 0:
		anim.play("Side_Idle")
if dir == "left":
	anim.flip_h = true
	if movement == 1:
		anim.play("Side_Walk")
	elif movement == 0:
		anim.play("Side_Idle")
	
if dir == "down":
	anim.flip_h = true
	if movement == 1:
		anim.play("Front_Walk")
	elif movement == 0:
		anim.play("Front_Idle")
if dir == "up":
	anim.flip_h = true
	if movement == 1:
		anim.play("Back_Walk")
	elif movement == 0:
		anim.play("Back_Idle")

Here is the link for the video:
https://www.youtube.com/watch?v=9u59U-DWNJs&list=PL3cGrGHvkwn0zoGLoGorwvGj6dHCjLaGd&index=4

Sorry for not providing this information earlier.

KennethM | 2023-07-07 06:20

:bust_in_silhouette: Reply From: PampoenKoekie

I might be wrong but maybe it could send you in the rigth direction. Try the move_and_collide() instead of move_and_slide()… I have had some slight troubles with the latter in Godot 4.

func _physics_process(delta):
    targetPosition = (Globals.playerPosition - position).normalized()
    move_and_collide(targetPosition * enemySpeed)

This is how I got my enemy to follow the player position.
targetPosition is an empty variable in the enemy script storing the above to it. In the singleton or global script I ask the playerPosition. See if this might help you in any way. All the best for your endeavours.