Area 2D not colliding.

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

I am creating a game like Tetris. I created the barriers/walls using Staticbody 2D and the blocks using Area 2D. When I run it the blocks pass through the Staticbody 2D without collission.How can I stop the falling blocks(Area 2D) when it touches the Staticbody 2D and stay at the top of the wall ?

Did you try to use Layers and Masks?

Nick888 | 2019-06-24 16:10

Just so that we’re on the same page: you do know that Area2D is for detecting collisions, and not really for stopping collisions?

Ertain | 2019-06-24 22:38

You are totally right. My own mistake. Probably has to replace the Aread2D with KinematicBodies2D.

Nick888 | 2019-06-25 03:33

1 Like
:bust_in_silhouette: Reply From: bodicpen

Hi there! Area2D is basically what its name is, an AREA. It’s mostly used for detecting collisions (detecting whether something entered an area, collided with an area, etc.) and while you can move this Area2D, it will not react to collisions but it may give you an output if you give it a script to do so.

You may want to replace the Area with a type of physics body (Kinematic, Rigid, Static)

Kinematic - this is used when you want an object to be solely controlled by you and its script.

Rigid - Rigid bodies are affected by physics whether they have scripts or not in them. Example, Angry Birds. You launch a bird to stones, wood, and pigs and they act like real physical objects in terms of falling, sliding, bouncing, rolling, and without scripts in them.

Static - this is commonly used for background and walls since it’s not affected by physics hence its name “static”. However, objects can still collide with this.

As for your game, I recommend using kinematic body since you want it to be controlled by a script (I assume moving 1 tile-space for a certain amount of time). However, you may want to add an area2d as a child of this kinematic body to detect collision so that when the area2d detects collision, it can tell its parent (the kinematic body) to stop moving either by setting the velocity, speed, or whatever variable you put in to 0, or you can just make a true/false flag like (Note, this is just a pseudo-code):

var can_move = true

func _physics_process(delta):
if can_move == true:
Insert your code of action here

func _on_Area2D_body_entered(body):
can_move = false

This means that in the beginning, the body can move but if another body (or area, depends on your game) touches it, the can_move will be set to false and will stop the body from moving.

For everyone who will get here with the same problem
Godot 4.2.1

Use correct signals

body_entered detect doesn’t detect the Area2d nodes, instead use area_entered signal or both if you need