Area2D Collision Not Working

Godot Version

`4.3
Here is my collision problem:

Here are my player and enemy scripts:

Player

extends CharacterBody2D

var move_speed :float = 100
var jump_force :float = 200
var gravity :float = 500

func _physics_process(delta):
	if not is_on_floor():
		velocity.y += gravity * delta

	velocity.x = 0
	
	if Input.is_key_pressed(KEY_LEFT):
		velocity.x -= move_speed
		
	if Input.is_key_pressed(KEY_RIGHT):
		velocity.x += move_speed
		
	if Input.is_key_pressed(KEY_SPACE) and is_on_floor():
		velocity.y = -jump_force
		
		
	move_and_slide()
	
	if global_position.y > 100:
			game_over()
		
	
func game_over():
	get_tree().reload_current_scene()

Enemy

extends Area2D

@export var move_speed : float = 30.0
@export var move_dir : Vector2 

var start_pos : Vector2
var target_pos : Vector2
# Called when the node enters the scene tree for the first time.
func _ready():
	start_pos = global_position
	target_pos = start_pos + move_dir


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	global_position = global_position.move_toward(target_pos, move_speed * delta)
	
	if global_position == target_pos:
		if global_position == start_pos:
			target_pos = start_pos + move_dir
		else:
			target_pos = start_pos
		





func _on_body_entered(body: Node2D) -> void:
	if body.is_in_group("Player"):
		print("Collision Working")
		body.game_over()

Here is a screenshot of my Godot Interface:


I am having the following repeated errors(120) in my debugger:

E 0:00:02:0074 create_tile: Cannot create tile. The tile is outside the texture or tiles are already present in the space the tile would cover.
<C++ Error> Condition “!room_for_tile” is true.
<C++ Source> scene/resources/2d/tile_set.cpp:4963 @ create_tile()

E 0:00:02:0074 has_alternative_tile: The TileSetAtlasSource atlas has no tile at (20, 0).
<C++ Error> Condition “!tiles.has(p_atlas_coords)” is true. Returning: false
<C++ Source> scene/resources/2d/tile_set.cpp:5400 @ has_alternative_tile()

E 0:00:02:0074 create_alternative_tile: TileSetAtlasSource has no tile at (20, 0).
<C++ Error> Condition “!tiles.has(p_atlas_coords)” is true. Returning: TileSetSource::INVALID_TILE_ALTERNATIVE
<C++ Source> scene/resources/2d/tile_set.cpp:5348 @ create_alternative_tile()

can anyone help?

Hi
Have you checked the values ​​of your layers and collision masks on the player and the enemy?
I only collide with the layers that I have marked in my masks

Also the areas should have monitoring=“true”.

Monitoring is set to true, the layers and masks of both the player and enemy are set to 1

Hello
Maybe you don’t have the signal connected correctly. I would try to put a print in the body_entered:

func _on_body_entered(body: Node2D) -> void:
	print("Entered")
	if body.is_in_group("Player"):
		print("Collision Working")
		body.game_over()

And also to connect the signal from the ready:

func _ready():
	...
	body_entered.connect(_on_body_entered)

There’s something wrong with the function as there is indeed a body “entered” output every time there is a collision!

Then it is possible that the reload_current_scene() function is returning an error.
Output the return of that function to the console:
SceneTree — Godot Engine (stable) documentation in English

I checked that, everything seems to be normal. Additionally, the reload current scene function is working when the player falls down from the ground. The issue seems to be a bug, but to be honest, i dont have enough knowledge to confirm that!

I printed from the game over function and that is working ok!

I restarted Godot and its working now!

1 Like