Following a tutorial and having issues with 2D player/enemy collision

Godot Version

4.3.stable

Question

So I’m new to Godot, but not to scripting as a whole, so while this old GDQuest tutorial is outdated in several aspects, I have been able to troubleshoot and work around that to get things working as intended. The tutorial is for making an extremely basic 2D platformer, however when I got to the section for making the player bounce off the enemy after stomping on them, I was unable to make the player bounce.

The player is able to jump on the enemy and kill them, however this is handled as part of the enemies script, not the players, so success there doesn’t really mean much.

I’m not entirely sure where I went wrong here, it doesn’t report any error codes, so I assume this is just another instance of this tutorial being outdated, and the proper procedure just being harder to find than the rest.

The function that’s giving me trouble is _on_enemy_detector_area_entered, and the section of the video I’m going off of begins at 1:36:04. Any assistance would be greatly appreciated, and I can provide more information about the rest of my script if needed.

player.gd

extends Actor

@export var stomp_impulse: = 1000.0

func _on_enemy_detector_area_entered(area: Area2D) -> void:
	velocity = calculate_stomp_velocity(velocity, stomp_impulse)


func _physics_process(delta: float) -> void:
	var is_jump_interrupted: = Input.is_action_just_released("jump") and velocity.y < 0.0
	var direction: = get_direction()
	velocity = calculate_move_velocity(velocity, direction, speed, is_jump_interrupted)
	move_and_slide()

func get_direction() -> Vector2:
	return Vector2(
		Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),
		-1.0 if Input.is_action_just_pressed("jump") and is_on_floor() else 1.0
	)


func calculate_move_velocity(
		linear: Vector2,
		direction: Vector2,
		move: Vector2,
		is_cancelled: bool
	) -> Vector2:
	var out: = linear
	out.x = move.x * direction.x
	out.y += gravity * get_physics_process_delta_time()
	if direction.y == -1.0:
		out.y = move.y * direction.y
	if is_cancelled:
		out.y = 0.0
	return out


func calculate_stomp_velocity(linear: Vector2, impulse: float) -> Vector2:
	var out: = linear
	out.y = -impulse
	return out

I would suggest adding some print statements to the top of the troublesome functions e.g. print("_on_enemy_detector_area_entered() called") to confirm whether the function is being called/signal triggered.

If it’s not, you could double-check collision layers and masks on the area2Ds, whether the signals are connected and whether the collision shapes are positioned in a way which lets them overlap.

Thanks so much! Your suggestion about checking the collision layers and masks was exactly what I needed. I forgot to mark the collision layer for the area2D as masking the enemy layer, so the script was working as intended, but the node was checking the wrong layer for collisions.

1 Like

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