|
|
|
|
Reply From: |
TchnlgPsnt |
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.
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"):
get_tree().change_scene("res://Level_2")
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
get_input()
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):
Level_1
–BackgroundLevel_1
–TileMap
–Player
------Camera2D2
------The Wizard
------CollisionPolygon2D
–Level_Changer
------Hitbox
----------CollisionShape2D
Just to be clear about the signal: you link the hitbox’s collisionshape2d to itself, and not the player’s collisionpolygon2d, right?