Godot 4
Hi! I am a newbie that played around with Godot for a bit and started his first serious project.
I have a scene that has a Node2D as parent its child is an Area2D that has as a child a RigidBody2D with a sprite2D and a collision shape as childs.
My objective was to make the gift sprite (and its CollisionShape2D and everything else obviously) fall to the ground and make it disappear so i programmed the node as:
extends Node2D
@onready var rigid_body = $RigidBody2D
func _ready():
pass
func _on_body_entered(body):
queue_free() # This will remove the entire Node2D instance
and gave area2D gravity with
extends Area2D
var velocity = Vector2()
func _physics_process(delta):
velocity.y += gravity * delta
position.y += velocity.y * delta
but the gifts kept weirdly passing through floor (a CollisionShape2D set as WorldBoundary) slowly and in a buggy way and disappearing under it then, sometimes reappearing when another gift was falling.
For some reason the sprites are falling under but the CollisionShape2D remains, making the other sprites fall and collide with those as well.
As a new user i cannot unfortunately share a video and I have no idea on how to show it.
So, first why do you have a rigid body as a child of an area2d?
You also shouldnt have to set position directly on rigidbody since it automatically interacts with physics engine.
If you want a falling gift to disappear when hitting floor, set up something like this
World → Node2D
As children
Floor → can be an area2d with collisionshape
Gift → rigidbody2d with sprite and collisionshape
Detect body_entered with area2d. Call body.destroy()
Rigidbody have a func called destroy() where you queue_free()
Rigidbody should ìnteract with the gravity and fall automatically without code.
Maybe, I am not understanding what you are trying to accomplish.
** As a side note, you shouldn’t double multiply by delta. You are multiplying velocity.y = gravity * delta, then you take position.y = velocity.y * delta which effectively is multiplying position twice by delta. Though this is unnecessary with what i suggested.
1 Like
Thank you! I think you understood what I was trying to do!
I know I shouldn’t have used an Area2D as a parent of a RigidBody2D but I was super confused because the gift scene and the scene where the floor is are two different scenes and was unsure on how to work it out.
And I coded the Area2D’s gravity because my objective was to make the Area2D fall, not the rigidbody as I was convinced that making so would help me use its signal to delete the instances of the gift scene that are made.
You maybe solved that, I’ll tell you once I test it, at least you noticed something else now that I gave this detail about the scenes that I previously forgot.
1 Like