#[beginner] collisions not working

6 messages · Page 1 of 1 (latest)

keen vine
#

I've been trying to make collisions work for roughly 2 hours now and still haven't found a solution. (no errors)

extends CharacterBody2D

@export var speed = 100
@export var accel = 10

@onready var coll = $Area2D
@onready var itemC = load("res://scripts/Item.gd")

func _physics_process(_delta) -> void:
    if Input.is_action_pressed("menu"):
        get_tree().quit()
    
    var direction: Vector2 = Input.get_vector("left", "right", "up", "down")
    
    velocity.x = move_toward(velocity.x, speed * direction.x, accel)
    velocity.y = move_toward(velocity.y, speed * direction.y, accel)
    
    move_and_slide()
    
    look_at(get_global_mouse_position())

func _on_ready():
    coll.monitoring = true
    coll.connect("area_shape_entered", hit)

func hit(body):
    print("AAA")
    if body.is_in_group("Item"):
        print("item")

#

the scene that I am trying to detect:

surreal quartz
#

It looks like you have the wrong signal connected. The signal that you are currently connecting is the area_shape_entered signal, and it returns four parameters: area_rid, area, area_shape_index and local_shape_index. You can simply just switch the signal to area_entered instead to only receive the reference to the item's area2d node, and how you have the signal's callback is to only receive one parameter, area_rid even though it says body. To make it more clear I would also recommend changing the body to area.

#

Changing the signal to area_entered means that you can keep the hit function as-is and receive the area2d node

#

Basically replace coll.connect("area_shape_entered", hit) with `coll.connect("area_entered", hit)

keen vine