0 votes

Hi, I am trying to figure out how to switch levels in a game I made. I used this script

extends Sprite

func _on_Hitbox_area_entered(area: Area2D) -> void:
    if area.is_in_group("Player"):

Nothing happens when the player collides with the Hitbox. Does anyone know what I am doing wrong -- is there a good tutorial on this?

Godot version v3.2.3
in Engine by (14 points)

I don't see anything wrong with the code here. Can you provide more information? Did you connect the signal properly?

Please see my reply to TchnlgPsnt below for more information--thanks for any help you can give.

1 Answer

0 votes

I see that your script extends Sprite, so I'm thinking maybe the collision isn't being picked up. The _on_Hitbox_area_entered function will only detect nodes of type Area2D that enter it, so try making sure that the node you want to collide with the hitbox is of type Area2D and has a CollisionShape2D or CollisionPolygon2D child.

Alternatively, you could instead use the on_body_entered signal to detect when physicsbodies enter the hitbox. If your player is a physicsbody, you will have more control over its movement. In this case, both nodes involved in the collision would still need to have a CollisionShape2D or CollisionPolygon2D child.

by (130 points)

Thank you for your help,

Yes, the collision is definitely not being picked up; you are right.

I have a Collisionpolygon2D for my Player and a CollisionShape2D for my Sprite as children.

Unfortunately it is still not working--and I don't an error message, the player just passes right through the Sprite. Here is the code as it looks now:

extends Sprite

func on_body_entered(area: KinematicBody2D) -> void:
    if area.is_in_group("Player"):

And, this is the script for my Player (in case this helps solve the problem):

    extends KinematicBody2D

var run_speed = 400
var jump_speed = -1100
var gravity = 2000

var velocity = Vector2()

export (int) var speed = 200

func get_input():
    velocity.x = 0
    var right = Input.is_action_pressed('right')
    var left = Input.is_action_pressed('left')
    var jump = Input.is_action_just_pressed('jump')

    if is_on_floor() and jump:
        velocity.y = jump_speed
    if right:
        velocity.x += run_speed
    if left:
        velocity.x -= run_speed

func _physics_process(delta):
    velocity.y += gravity * delta
    velocity = move_and_slide(velocity, Vector2(0, -1))

I used a signal to link the on_body_entered and connected it to my Sprite. Not sure if I connected it correctly.

Many thanks for any other thoughts!!

The scene tree is set up as (again not sure if this is helpful):

------The Wizard

Just to be clear about the signal: you link the hitbox's collisionshape2d to itself, and not the player's collisionpolygon2d, right?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.