パハットノート

主にUnity(C#)を中心としたプログラムの備忘録

Unity Physics2D.OverlapBoxの使い方




Physics2D.OverlapBoxについて

概要

  • 衝突判定に使える。
  • 検出対象にはBoxCollider2Dなどをアタッチしておく必要がある。
  • 指定した四角形の範囲内にある重なっているBoxCollider2Dなどを検出することで、衝突判定を行う。
  • OnCollisionStay2Dなどと異なり、任意のタイミングで呼び出せる



使用する関数の説明

Physics2D.OverlapBox(Vector2 point, Vector2 size, float angle, [int layerMask])

  • 戻り値の型はCollider2D
  • pointは四角形の中心座標、sizeは大きさ、angleは傾きを示す。
  • LayerMaskで検知するLayerを指定できる。省略可能(その場合はすべてのLayerの衝突を検知する)。




使用例

準備

以降の例で使用するオブジェクトを示します。

  • BoxCollider2Dは初期設定のままです。
  • 「Black」のLayerの設定を忘れないように
  • 「Star」にアタッチされている「TestController.cs」はキーボードからの入力で移動できるようにするためのもので、次のプログラムが記載されています。




using UnityEngine;

public class TestController : MonoBehaviour
{
    [SerializeField] float moveSpeed;

    private void Update()
    {
        if (Input.GetKey(KeyCode.RightArrow))
        {
            gameObject.transform.Translate(Vector2.right * moveSpeed * Time.deltaTime, Space.World);
        }
        else if (Input.GetKey(KeyCode.LeftArrow))
        {
            gameObject.transform.Translate(Vector2.left * moveSpeed * Time.deltaTime, Space.World);
        }
        else if (Input.GetKey(KeyCode.DownArrow))
        {
            gameObject.transform.Translate(Vector2.down * moveSpeed * Time.deltaTime, Space.World);
        }
        else if (Input.GetKey(KeyCode.UpArrow))
        {
            gameObject.transform.Translate(Vector2.up * moveSpeed * Time.deltaTime, Space.World);
        }
    }
}




①LayerMaskの指定なし

引数のLayerMaskを省略した場合の使用方法です。
先ほど用意した「Star」に以下のスクリプトをアタッチして設定を行います。

スクリプト
  • 関数の「DrawOverlapBox」によりゲーム実行中にシーンビュー上でOverlapBoxの四角形が描画されます。
  • そのとき、衝突検知をしていないと「defaultColor」の色に、していると「overlapColor」で指定した色になります。
using UnityEngine;

public class OverlapBoxTest : MonoBehaviour
{
    [SerializeField] Vector2 size;
    [SerializeField] Color defaultColor;
    [SerializeField] Color overlapColor;

    void Update()
    {
        Collider2D col = Physics2D.OverlapBox(gameObject.transform.position, size, 0f);
        if (col)
        {
            DrawOverlapBox(gameObject.transform.position, size, overlapColor);
        }
        else
        {
            DrawOverlapBox(gameObject.transform.position, size, defaultColor);
        }
    }

    void DrawOverlapBox(Vector2 point, Vector2 size, Color color)
    {
        //4つの頂点の位置を取得
        Vector2 rightUp = new Vector2(point.x + size.x / 2, point.y + size.y / 2);//右上
        Vector2 leftUp = new Vector2(point.x - size.x / 2, point.y + size.y / 2);//左上
        Vector2 leftDown = new Vector2(point.x - size.x / 2, point.y - size.y / 2);//左下
        Vector2 rightDown = new Vector2(point.x + size.x / 2, point.y - size.y / 2);//右下
            
        //描画
        Debug.DrawLine(rightUp, leftUp, color);
        Debug.DrawLine(leftUp, leftDown, color);
        Debug.DrawLine(leftDown, rightDown, color);
        Debug.DrawLine(rightDown, rightUp, color);
    }
}



設定




実行結果




②LayerMaskの指定あり

引数のLayerMaskを指定した場合の使用方法です。
先ほど用意した「OverlapBoxTest」を以下のように書き換えます

スクリプト
using UnityEngine;

public class OverlapBoxTest : MonoBehaviour
{
    //変更箇所1
    [SerializeField] LayerMask blackLayer;

    [SerializeField] Vector2 size;
    [SerializeField] Color defaultColor;
    [SerializeField] Color overlapColor;

    void Update()
    {
        //変更箇所2
        Collider2D col = Physics2D.OverlapBox(gameObject.transform.position, size, 0f, blackLayer);


        if (col)
        {
            DrawOverlapBox(gameObject.transform.position, size, overlapColor);
        }
        else
        {
            DrawOverlapBox(gameObject.transform.position, size, defaultColor);
        }
    }

    void DrawOverlapBox(Vector2 point, Vector2 size, Color color)
    {
        //4つの頂点の位置を取得
        Vector2 rightUp = new Vector2(point.x + size.x / 2, point.y + size.y / 2);//右上
        Vector2 leftUp = new Vector2(point.x - size.x / 2, point.y + size.y / 2);//左上
        Vector2 leftDown = new Vector2(point.x - size.x / 2, point.y - size.y / 2);//左下
        Vector2 rightDown = new Vector2(point.x + size.x / 2, point.y - size.y / 2);//右下

        //描画
        Debug.DrawLine(rightUp, leftUp, color);
        Debug.DrawLine(leftUp, leftDown, color);
        Debug.DrawLine(leftDown, rightDown, color);
        Debug.DrawLine(rightDown, rightUp, color);
    }
}



設定




実行結果