Check if player touching trap

Godot Version

v4.5.dev1

Question

I defined a class for istantiating the player and set name for refering to it purpose:the scrpt I wrote is:

extends CharacterBody2D

class_name PlayerController
...
...
...

further I defined the Trap script:

extends Node2D


func _on_area_2d_body_entered(body: Node) -> void:
	if body is PlayerController:
		print('body is PlayerController')
	else:
		print('body NOT a PlayerController')

altough moving the player I get into the area, the script allert me: “body NOT a PlayerController”

How do you instantiate the Player? Can you show that piece of code?

Code is solid so that’s not the issue. Things that come to mind are:

  1. You’re instancing from a different scene than the one you think you are pointing to
  2. Your script is not properly attached to the root node of the player scene
  3. Scene tree of your character isn’t the correct hierarchy (e.g. another physics body, like a weapon or something, attached to your scene enters)

You can bugfix all by printing the body that triggers the signal. That should give you a first look at least.

Over time I’ve come to prefer adding scenes to groups to control these game logic interactions instead of relying on class names because groups are exposed to the editor, including real time bug-fixing.

following how player item appear:

code of the attached script is:

extends CharacterBody2D

class_name PlayerController

@export var SPEED = 120.0
@export var JUMP_VELOCITY = -300.0
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D

func _physics_process(delta: float) -> void:
	
	if Input.is_action_pressed("move_left"):
		velocity.x = -SPEED 
		animated_sprite.flip_h = true
		animated_sprite.play("walking")
	elif Input.is_action_pressed("move_right"):
		velocity.x = SPEED
		animated_sprite.flip_h = false
		animated_sprite.play("walking")
	
	else:
		animated_sprite.play("idle")
		velocity.x = 0
	#handlig jump and gravity
	if is_on_floor() :
		#adding jump through input key for jump
		if Input.is_action_just_pressed("jump"):
			velocity.y = JUMP_VELOCITY
	else:
		#applying gravity 
		velocity += get_gravity() * delta
		animated_sprite.play("jump")
		
	move_and_slide()

There’s no code for instantiating the player here, whereas you mentioned you instantiate the player somehow.
Or is the player just included in the main scene already? If so, can you check that the player has a script attached to it and that it’s still attached during runtime?

I think player is included in the main scene:


How should I check the scrpt is still attached suring runtime??..

Are you sure it’s detecting your player and not something else, like an Enemy? You could try adding the print statement to see what comes up.

func _on_area_2d_body_entered(body: Node) -> void:
	print(body)
	if body is PlayerController:
		print('body is PlayerController')
	else:
		print('body NOT a PlayerController')
1 Like

indeed you are right;what is printed out is:
body is: items:<TileMapLayer#58166610840>
body NOT a PlayerController
body is: terrain:<TileMapLayer#58149833623>
body NOT a PlayerController
body is: items:<TileMapLayer#58116277597>
body NOT a PlayerController
body is: terrain:<TileMapLayer#58099500384>
body NOT a PlayerController
mmm it is not clear to me is how checking what is dectected is just the player (class_name PlayerController) :thinking:

Your code would work correctly, if only the area would properly detect the player. Right now, as you can see, it’s not detecting the player at all.
Check collision layers and masks on both your player and the area, as well as make sure the collision shapes are properly defined and that the area is actually touching the player (maybe the collision shape is too small and you think they should be touching, but they aren’t?)

You can turn on Visible Collision Shapes to help you debug the issues.