Up to date

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

Area2Dの使用

はじめに

Godotは、衝突の検出と応答の両方を提供する多数のコリジョンオブジェクトを提供します。プロジェクトに使用するものを決定しようとすると、混乱する可能性があります。問題のそれぞれがどのように機能し、その長所と短所が何であるかを理解すれば、問題を回避して開発を簡素化できます。このチュートリアルでは、Area2D ノードを見て、その使用方法の例をいくつか示します。

注釈

このドキュメントは、あなたがGodotのさまざまな物理ボディに精通していることを前提としています。最初に 物理の紹介 をお読みください。

Areaとは何ですか?

Area2Dは、2D空間の領域を定義します。このスペースでは、他の CollisionObject2D ノードが進入、オーバラップ、および退出することを検出できます。Areaでは、ローカルの物理プロパティをオーバーライドすることもできます。これらの各機能を以下で説明します。

Areaのプロパティ

Areaには、動作をカスタマイズするために使用できる多くのプロパティがあります。

../../_images/area2d_properties.webp

The Gravity, Linear Damp, and Angular Damp sections are used to configure the area's physics override behavior. We'll look at how to use those in the Area influence section below.

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

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. The Collision section of CollisionObject2D is where you configure the area's collision layer(s) and mask(s).

オーバーラップ検出

おそらく、Area2Dノードの最も一般的な使用方法は、接触とオーバーラップの検出です。 2つのオブジェクトが触れたが、物理的な衝突は必要ないことを知る必要がある場合、Areaを使用して連絡先に通知できます。

たとえば、プレイヤーが受け取るコインを作っているとしましょう。 コインは堅固なオブジェクトではありません - プレイヤーはそれの上に立ったり押したりすることはできません - プレイヤーが触れたときにコインが消えるようにしたいだけです。

コインのノード設定は次のとおりです:

../../_images/area2d_coin_nodes.webp

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.

注釈

シグナルの使用に慣れていない場合は、概要について シグナルの使用 を参照してください。

extends Area2D

func _on_coin_body_entered(body):
    queue_free()

今、私たちのプレイヤーは、コインを収集することができます!

別の使用例:

  • Areaは、弾丸やその他の発射物が命中してダメージを与える場合に最適ですが、反発などの物理は必要ありません。

  • 敵の周囲の大きな円形エリアを使用して、その"detect"(検出)半径を定義します。 プレイヤーがエリアの外にいるとき、敵はそれを「見る」ことができません。

  • 「セキュリティカメラ」- 複数のカメラがある大規模なレベルでは、各カメラにAreaを取り付けて、プレイヤーが入るときにそれらをアクティブにします。

See the 最初の2Dゲーム for an example of using Area2D in a game.

Areaの影響

The second major use for area nodes is to alter physics. By default, the area won't do this, but you can enable this with the Space Override property. When areas overlap, they are processed in Priority order (higher priority areas are processed first). There are four options for override:

  • Combine - Areaは、これまでに計算された値にその値を加算します。

  • Replace - Areaは物理特性を置換し、優先度の低いAreaは無視されます。

  • Combine-Replace - Areaは、これまでに計算されたものに優先度の高い値を加算し、優先度の低いAreaを無視します。

  • Replace-Combine - このAreaは、これまでに計算された重力/減衰を置き換えますが、残りのAreaの計算を続けます。

これらのプロパティを使用すると、複数の重なり合う領域を持つ非常に複雑な動作を作成できます。

オーバーライドできる物理特性は次のとおりです:

  • Gravity - Area内の重力の強さ。

  • Gravity Direction - This vector does not need to be normalized.

  • Linear Damp - オブジェクトの減速量 - 毎秒 linear velocity からこの値が失われます。

  • Angular Damp - オブジェクトの回転の減速量 - 毎秒 angular velocity からこの値が失われます。

ポイント重力

The Gravity Point property allows you to create an "attractor". Gravity in the area will be calculated towards a point, given by the Point Center property. Values are relative to the Area2D, so for example using (0, 0) will attract objects to the center of the area.

以下に添付されているサンプルプロジェクトには、物理オーバーライドを示す3つの領域があります。

../../_images/area2d_override.gif

You can download this project here: area_2d_starter.zip