Hi!
I started learning how to use Godot recently and I'm currently trying to create a first project that is a puzzle game on a gridmap when everytime you move from a cell to another, every enemies moves according to a "pattern" (think Crypt of the Necrodancer movement system but without the rythm part).

I started thinking and working about the game and its mechanics 3 days ago, trying to understand how I could code such a thing since I'm a beginner. I found kidscancode, it helped me with the grid-based movements and while I still have trouble to understand some things (like tween), it's much more clear in my mind now.

Anyway, here is my problem: as I said I want the enemies to move when the player is moving. Also, if the player is colliding with a wall, I don't want the enemies to move.
So far I've almost succeeded, even if I think the code I tried to re-wrote with the adjustements I wanted is far from perfect. But everytime I try to walk into a wall, the enemy is still moving. I want it to stop.

I tried everything that came to my mind for the past 2 days before reaching out here for your help. Any advice is welcome!

Here is the two script I wrote. The first one is for the player, the other is for enemy.

PLAYER SCRIPT

extends CharacterBody2D

var animation_speed = 10
var moving = false
var coll = 0
var tile_size = 64
var inputs = {"ui_right": Vector2.RIGHT,
			"ui_left": Vector2.LEFT,
			"ui_up": Vector2.UP,
			"ui_down": Vector2.DOWN}

func _ready():
	pass

func _unhandled_input(event):
	if moving:
		return
	for dir in inputs.keys():
		if event.is_action_pressed(dir): 
			coll = 0
			player_move(dir)
			print(coll)

@onready var ray = $RayCast2D

func player_move(dir):
	ray.target_position = inputs[dir] * tile_size
	ray.force_raycast_update()
	if !ray.is_colliding():
		var tween = get_tree().create_tween()
		tween.tween_property(self, "position", position + inputs[dir] * tile_size, 1.0/animation_speed).set_trans(Tween.TRANS_SINE)
		moving = true
		$AnimationPlayer.play(dir)
		await tween.finished
		moving = false
		coll = 0
	
	elif ray.is_colliding:
		coll = 1

ENEMY SCRIPT

extends CharacterBody2D

var animation_speed = 10
var moving = false
var tile_size = 64
var vec = Vector2(-1,0)
var inputs = {"ui_right": Vector2.RIGHT,
			"ui_left": Vector2.LEFT,
			"ui_up": Vector2.UP,
			"ui_down": Vector2.DOWN}

func _ready():
	pass

@onready var player_col = get_parent().get_node("Player").get("coll")

func _unhandled_input(event):
	if moving:
		return
	for dir in inputs.keys():
		if player_col == 0 and event.is_action_pressed(dir):
			enemy_move(dir)


@onready var ray2 = $RayCast2D


func enemy_move(dir):
	ray2.target_position = vec * tile_size
	ray2.force_raycast_update()
	if !ray2.is_colliding():
		var tween = get_tree().create_tween()
		tween.tween_property(self, "position", position + vec * tile_size, 1.0/animation_speed).set_trans(Tween.TRANS_SINE)
		moving = true
		$AnimationPlayer.play(dir)
		await tween.finished
		moving = false

	elif ray2.is_colliding():
		vec.x = vec.x * -1
		var tween = get_tree().create_tween()
		tween.tween_property(self, "position", position + vec * tile_size, 1.0/animation_speed).set_trans(Tween.TRANS_SINE)
		moving = true
		$AnimationPlayer.play(dir)
		await tween.finished
		moving = false



func _on_area_2d_area_entered(area):
	get_tree().reload_current_scene()

I hope someone could help me with this! Thanks a lot in advance! ☺

  • Raytro Enemy movement shouldn't be triggered by input. Only player should respond to input and then if the player can move, its script should call enemy's enemy_move() method. That way the enemy will only move when the player moves.

    Also stay away from await if you're a beginner. Don't use it.

I'm posting again because I have the impression that the thread has gone unnoticed. Sorry if this is not allowed.

  • xyz replied to this.

    Raytro Enemy movement shouldn't be triggered by input. Only player should respond to input and then if the player can move, its script should call enemy's enemy_move() method. That way the enemy will only move when the player moves.

    Also stay away from await if you're a beginner. Don't use it.

      xyz omg thank you SO MUCH it works great now! Really appreciated 😊