how to destroy a staticbody2d

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Player_0

as the title says what I want to achieve is: eliminate a staticbody2d that fulfills the function of wall / wall within my game, eliminate it when another object which is a kinematicbody2d that fulfills the role of a missile hits it; the main problem I have is how to detect the collision in the staticbody2d. It should be noted that the layers and masks are configured perfectly

:bust_in_silhouette: Reply From: frannydancis

The technique I used to accomplish this should be applicable to this situation:

Firstly, you want to make sure that your staticbody2D is actually an Area2D. This is because the Area2D node has the signal, “body_entered,” which we will be using to detect collisions.

After creating an Area2D node, create a collisionshape2D that will serve as the “hitbox” of your wall, for your case it should cover the wall entirely I assume.

Connect the “body_entered” signal from Area2D to the script of your choosing.

func _on_Area2D_body_entered(body): 
    #Anything you want to happen before the wall is gone above queue_free()

To specify the kinematicbody2D, you can do the following:

func _on_Area2D_body_entered(body): 
    if body == "Missile":

sorry if my english is so bad, thanks for your answer, but if i change the type of my staticbody2d, lost the property for collide with my player, this like say that the function of be a wall lost and i dont want that lost this function

Player_0 | 2021-04-01 00:26

In that case, here is a step-by-step solution:

  1. Create an Area2D
  2. Add a StaticBody2D as a child of Area2D
  3. Add a CollisionShape2D as a child of the StaticBody2D
  4. Add a CollisionShape2D as a child of Area2D

Now, it should have the functionality of the wall as well as be destructible using the code above.

frannydancis | 2021-04-01 00:37

exactly, i had not thought of it , thank you for the solution brou

Player_0 | 2021-04-01 00:42

Of course! Good luck and happy developing :slight_smile:

frannydancis | 2021-04-01 00:45