---
tags:
    - "Ray"
    - "Line"
    - "Sight"
    - "Cast"
    - "3D"
---

# Raycast 3D

[Physics 3D](index.md) / **Raycast 3D**

<div class="grid cards" markdown>

* :green_circle: Can Read

* :red_circle: Cannot Write

</div>

## Description

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

## Parameters

| Name | Description |
| :--- | :---------- |
| Point | The starting point of the ray in 3D 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

```cs
using System;
using System.Collections.Generic;
using UnityEngine;
using VisualScript.Icons;

namespace VisualScript.Runtime
{
    [Get]
    
    [Title("Raycast 3D")]
    [Category("Physics 3D/Raycast")]
    
    [Description("Casts a ray against 3D 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 3D 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", "3D")]
    
    [Serializable]
    public class TypeCollectionGameObjectsPhysics3DRaycast : TypeCollectionGameObjects
    {
        private static readonly Color GIZMO = new Color(0f, 1f, 0f, 0.25f);
        
        private static readonly RaycastHit[] HitsBuffer = new RaycastHit[128];
        
        [SerializeField] private GetVector3 m_Point = new GetVector3();
        [SerializeField] private GetVector3 m_Direction = new GetVector3(Vector3.forward);
        [SerializeField] private GetNumber m_Distance = new GetNumber(100f);
        
        [SerializeField] private LayerMask m_LayerMask = Physics.DefaultRaycastLayers;
        [SerializeField] private GetBool m_UseTriggers = new GetBool(false);
        
        public override void Get(Args args, List<GameObject> values)
        {
            Vector3 point = this.m_Point.Get(args);
            Vector3 direction = this.m_Direction.Get(args);
            float distance = (float) this.m_Distance.Get(args);
            
            QueryTriggerInteraction triggerInteraction = this.m_UseTriggers.Get(args) 
                ? QueryTriggerInteraction.Collide 
                : QueryTriggerInteraction.Ignore;
            
            int numHits = Physics.RaycastNonAlloc(
                point,
                direction,
                HitsBuffer,
                distance,
                this.m_LayerMask,
                triggerInteraction
            );
            
            Vector3 endPoint = point + (direction.normalized * distance);
            GizmosManager.Line(point, endPoint, GIZMO, 0.25f);
            
            values.Clear();
            for (int i = 0; i < numHits; ++i)
            {
                Collider collider = HitsBuffer[i].collider;
                if (collider != null) values.Add(collider.gameObject);
            }
            
            Array.Clear(HitsBuffer, 0, numHits);
        }

        public override string ToString()
        {
            return $"Raycast 3D from {this.m_Point} direction {this.m_Direction}";
        }
        
        protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
    }
}
```

