Overlap Capsule 2D#
Physics 2D / Overlap Capsule 2D
Can Read
Cannot Write
Description#
Returns all 2D colliders that overlap with a capsule
Parameters#
| Name | Description |
|---|---|
| Point | The center point of the capsule |
| Size | The size (width and height) of the capsule |
| Direction | The direction of the capsule (Vertical or Horizontal) |
| Angle | The rotation angle of the capsule 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 Capsule 2D")]
[Category("Physics 2D/Overlap Capsule")]
[Description("Returns all 2D colliders that overlap with a capsule")]
[Image(typeof(IconCapsuleOutline), ColorTheme.Type.Lime, typeof(BadgePhysics))]
[Parameter("Point", "The center point of the capsule")]
[Parameter("Size", "The size (width and height) of the capsule")]
[Parameter("Direction", "The direction of the capsule (Vertical or Horizontal)")]
[Parameter("Angle", "The rotation angle of the capsule 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", "Capsule", "Pill", "Oblong")]
[Serializable]
public class TypeCollectionGameObjectsPhysics2DOverlapCapsule : 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 CapsuleDirection2D m_Direction = CapsuleDirection2D.Vertical;
[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.OverlapCapsule(
point,
size,
this.m_Direction,
angle,
new ContactFilter2D
{
useLayerMask = true,
layerMask = this.m_LayerMask,
useTriggers = this.m_UseTriggers.Get(args)
},
colliders
);
GizmosManager.Capsule2D(point, Quaternion.Euler(0f, 0f, angle), size, this.m_Direction, 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 Capsule at {this.m_Point} size {this.m_Size} direction {this.m_Direction} angle {this.m_Angle}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}