Up to date

This page is up to date for Godot 4.0. If you still find outdated information, please open an issue.

Using Area2D

Introduction

Godot offers a number of collision objects to provide both collision detection and response. Trying to decide which one to use for your project can be confusing. You can avoid problems and simplify development if you understand how each of them works and what their pros and cons are. In this tutorial, we'll look at the Area2D node and show some examples of how it can be used.

Note

This document assumes you're familiar with Godot's various physics bodies. Please read Physics introduction first.

What is an area?

An Area2D defines a region of 2D space. In this space you can detect other CollisionObject2D nodes overlapping, entering, and exiting. Areas also allow for overriding local physics properties. We'll explore each of these functions below.

Area properties

Areas have many properties you can use to customize their behavior.

../../_images/area2d_properties.png

The first eight properties are used to configure the area's physics override behavior. We'll look at how to use those in the section below.

Monitoring and Monitorable are used to enable and disable the area.

The "Collision" section is where you configure the area's collision layer(s) and mask(s).

The "Audio Bus" section allows you to override audio in the area, for example to apply an audio effect when the player moves through.

Note that Area2D extends CollisionObject2D, so it also provides properties inherited from that class, such as input_pickable.

Overlap detection

Perhaps the most common use of Area2D nodes is for contact and overlap detection. When you need to know that two objects have touched, but don't need physical collision, you can use an area to notify you of the contact.

For example, let's say we're making a coin for the player to pick up. The coin is not a solid object - the player can't stand on it or push it - we just want it to disappear when the player touches it.

Here's the node setup for the coin:

../../_images/area2d_coin_nodes.png

To detect the overlap, we'll connect the appropriate signal on the Area2D. Which signal to use depends on the player's node type. If the player is another area, use area_entered. However, let's assume our player is a CharacterBody2D (and therefore a CollisionObject2D type), so we'll connect the body_entered signal.

Note

If you're not familiar with using signals, see Using signals for an introduction.

extends Area2D

func _on_coin_body_entered(body):
    queue_free()