Overlap Box 2D#
Physics 2D / Overlap Box 2D
Can Read
Cannot Write
Description#
Returns all 2D colliders that overlap with a box
Parameters#
| Name | Description |
|---|---|
| Point | The center point of the box |
| Size | The size (width and height) of the box |
| Angle | The rotation angle of the box in degrees |
| Layer Mask | Filter to check objects only on specified layers |
| Use Triggers | Whether to check for Colliders marked as triggers or not |
Code#
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Pool;
using VisualScript.Icons;
namespace VisualScript.Runtime
{
[Get]
[Title("Overlap Box 2D")]
[Category("Physics 2D/Overlap Box")]
[Description("Returns all 2D colliders that overlap with a box")]
[Image(typeof(IconSquareOutline), ColorTheme.Type.Lime, typeof(BadgePhysics))]
[Parameter("Point", "The center point of the box")]
[Parameter("Size", "The size (width and height) of the box")]
[Parameter("Angle", "The rotation angle of the box in degrees")]
[Parameter("Layer Mask", "Filter to check objects only on specified layers")]
[Parameter("Use Triggers", "Whether to check for Colliders marked as triggers or not")]
[Keywords("Inside", "Box", "Rectangle", "Square")]
[Serializable]
public class TypeCollectionGameObjectsPhysics2DOverlapBox : TypeCollectionGameObjects
{
private static readonly Color GIZMO = new Color(0f, 1f, 0f, 0.25f);
[SerializeField] private GetVector2 m_Point = new GetVector2();
[SerializeField] private GetVector2 m_Size = new GetVector2();
[SerializeField] private GetNumber m_Angle = new GetNumber(0f);
[SerializeField] private LayerMask m_LayerMask = Physics2D.DefaultRaycastLayers;
[SerializeField] private GetBool m_UseTriggers = new GetBool(false);
public override void Get(Args args, List<GameObject> values)
{
Vector2 point = this.m_Point.Get(args);
Vector2 size = this.m_Size.Get(args);
float angle = (float) this.m_Angle.Get(args);
List<Collider2D> colliders = ListPool<Collider2D>.Get();
int numColliders = Physics2D.OverlapBox(
point,
size,
angle,
new ContactFilter2D
{
useLayerMask = true,
layerMask = this.m_LayerMask,
useTriggers = this.m_UseTriggers.Get(args)
},
colliders
);
GizmosManager.Cube(point, Quaternion.Euler(0f, 0f, angle), size, GIZMO, GizmoMode.Outline, 0.25f);
values.Clear();
for (int i = 0; i < numColliders; ++i)
{
Collider2D collider = colliders[i];
if (collider != null) values.Add(collider.gameObject);
}
ListPool<Collider2D>.Release(colliders);
}
public override string ToString()
{
return $"Overlap Box at {this.m_Point} size {this.m_Size} angle {this.m_Angle}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}