Area 2D won't detect CharacterBody2D

Godot 4.4
I’m having issues with an Area2D detecting a CharacterBody2D node. When the player approaches the Area2D node, the print statement doesn’t happen.

Here is the code I’m using in the node tree with the Area2D:

var player_in_area = false


func _on_chat_detection_body_entered(body):
        if body.has_method("player"):
            print("player has entered speaking range")
            player_in_area = true

func _on_chat_detection_body_exited(body):
        if body.has_method("player"):
            print("player has exited speaking range")
            player_in_area = false

Here are the node trees of the dialogue area and the player:

Player (CharacterBody2D)
→ Sprite2D
→ Camera 2D
→ PlayerCollision (CollisionShape2D)

Speaker (CharacterBody2D)
→ Sprite2D
→ ChatDetection (Area2D)
→ -> CollisionShape2D

Does your player have a func player in it?

No, it doesn’t look like it.

This is the code inside the player:

extends CharacterBody2D


const SPEED = 600.0
const JUMP_VELOCITY = -600.0

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")

func _physics_process(delta):
	if not is_on_floor():
		velocity.y += gravity * delta
	if Input.is_action_just_pressed("ui_jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY
	var direction = Input.get_axis("ui_left", "ui_right")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()

I added one with a pass function, and it worked, thank you!

Your area only looks for objects with a func player in it, that’s what has_method("player") means. I’d recommend giving your player a class_name so you can reference that instead.

extends CharacterBody2D
class_name Player

Then your area can use

if body is Player:
2 Likes

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