Overlap Circle 2D#
Physics 2D / Overlap Circle 2D
Can Read
Cannot Write
Description#
Returns all 2D colliders that overlap with circle
Parameters#
| Name | Description |
|---|---|
| Colliders | The center of the circle |
| Radius | The radius of the circle |
| 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 Circle 2D")]
[Category("Physics 2D/Overlap Circle")]
[Description("Returns all 2D colliders that overlap with circle")]
[Image(typeof(IconCircleOutline), ColorTheme.Type.Lime, typeof(BadgePhysics))]
[Parameter("Colliders", "The center of the circle")]
[Parameter("Radius", "The radius of the circle")]
[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", "Sphere", "Circumference", "Round")]
[Serializable]
public class TypeCollectionGameObjectsPhysics2DOverlapCircle : TypeCollectionGameObjects
{
private static readonly Color GIZMO = new Color(0f, 1f, 0f, 0.25f);
[SerializeField] private GetVector2 m_Point = new GetVector2();
[SerializeField] private GetNumber m_Radius = new GetNumber(1f);
[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);
float radius = (float) this.m_Radius.Get(args);
List<Collider2D> colliders = ListPool<Collider2D>.Get();
int numColliders = Physics2D.OverlapCircle(
point,
radius,
new ContactFilter2D
{
useLayerMask = true,
layerMask = this.m_LayerMask,
useTriggers = this.m_UseTriggers.Get(args)
},
colliders
);
GizmosManager.Sphere(point, Quaternion.identity, radius, 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 Circle at {this.m_Point} radius {this.m_Radius}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}