Documentation index for AI agents (llms.txt) A Markdown version of every page is available: request the page's source under /docs/, follow the "alternate" link in this page's head, or start from /llms.txt.
Visual Script logo Manual

Raycast 2D#

Physics 2D / Raycast 2D

  • Can Read

  • Cannot Write

Description#

Casts a ray against colliders in the scene and returns all overlapped GameObjects

Parameters#

Name Description
Point The starting point of the ray in 2D space
Direction The direction vector of the ray
Distance The maximum distance the ray should check
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("Raycast 2D")]
    [Category("Physics 2D/Raycast")]
    
    [Description("Casts a ray against colliders in the scene and returns all overlapped GameObjects")]
    [Image(typeof(IconLineStartEnd), ColorTheme.Type.Lime, typeof(BadgePhysics))]
    
    [Parameter("Point", "The starting point of the ray in 2D space")]
    [Parameter("Direction", "The direction vector of the ray")]
    [Parameter("Distance", "The maximum distance the ray should check")]
    [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("Ray", "Line", "Sight", "Cast")]
    
    [Serializable]
    public class TypeCollectionGameObjectsPhysics2DRaycast : TypeCollectionGameObjects
    {
        private static readonly Color GIZMO = new Color(0f, 1f, 0f, 0.25f);
        
        [SerializeField] private GetVector2 m_Point = new GetVector2();
        [SerializeField] private GetVector2 m_Direction = new GetVector2(Vector2.right);
        [SerializeField] private GetNumber m_Distance = new GetNumber(100f);
        
        [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 direction = this.m_Direction.Get(args);
            float distance = (float) this.m_Distance.Get(args);
            
            List<RaycastHit2D> hits = ListPool<RaycastHit2D>.Get();
            
            int numHits = Physics2D.Raycast(
                point,
                direction,
                new ContactFilter2D
                {
                    useLayerMask = true,
                    layerMask = this.m_LayerMask,
                    useTriggers = this.m_UseTriggers.Get(args)
                },
                hits,
                distance
            );
            
            Vector2 endPoint = point + direction.normalized * distance;
            GizmosManager.Line(point, endPoint, GIZMO, 0.25f);
            
            values.Clear();
            for (int i = 0; i < numHits; ++i)
            {
                Collider2D collider = hits[i].collider;
                if (collider != null) values.Add(collider.gameObject);
            }
            
            ListPool<RaycastHit2D>.Release(hits);
        }

        public override string ToString()
        {
            return $"Raycast from {this.m_Point} direction {this.m_Direction}";
        }
        
        protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
    }
}
Visual Script logoVisual Script © Catsoft Works 2026. All rights reserved.