Assets/MRTK/Core/Utilities/Physics/ConeCastUtility.cs
/// <summary>
/// Function casts a sphere along a ray and checks if the hitpoint is within the angle of the cone and returns detailed information.
/// </summary>
/// <param name="origin">The vertex of the cone and the at the start of the sweep.</param>
/// <param name="direction">The direction into which to sweep the sphere..</param>
/// <param name="maxRadius">The radius of the sweep.</param>
/// <param name="maxDistance">The max length of the cast.</param>
/// <param name="coneAngle">The angle used to define the cone.</param>
/// <param name="layerMask">A Layer mask that is used to selectively ignore colliders when casting a capsule.</param>
/// <returns>An array of structs that contain RaycastHit, distance to hit, and the angle of all the objects that were hit.</returns>
Function casts a sphere along a ray and checks if the hitpoint is within the angle of the cone and returns detailed information.
Function casts a sphere along a ray and checks if the hitpoint is within the angle of the cone and returns detailed information .
public static ConeCastHit[] ConeCastAll(Vector3 origin, Vector3 direction, float maxRadius, float maxDistance, float coneAngle, LayerMask layerMask)
        {
            coneCastHitList.Clear();
            RaycastHit[] sphereCastHits = UnityEngine.Physics.SphereCastAll(origin - new Vector3(0, 0, maxRadius), maxRadius, direction, maxDistance, layerMask);
            if (sphereCastHits.Length == 0)
            {
                return Array.Empty<ConeCastHit>();
            }
            for (int i = 0; i < sphereCastHits.Length; i++)
            {
                Vector3 hitPoint = sphereCastHits[i].point;
                Vector3 directionToHit = hitPoint - origin;
                float angleToHit = Vector3.Angle(direction, directionToHit);
                if (angleToHit < coneAngle)
                {
                    coneCastHitList.Add(new ConeCastHit(sphereCastHits[i], directionToHit.magnitude, angleToHit));
                }
            }
            return coneCastHitList.ToArray();
        }
public static ConeCastHit [ ] ConeCastAll ( Vector3 origin , Vector3 direction , float maxRadius , float maxDistance , float coneAngle , LayerMask layerMask ) { coneCastHitList . Clear ( ) ; RaycastHit [ ] sphereCastHits = UnityEngine . Physics . SphereCastAll ( origin - new Vector3 ( 0 , 0 , maxRadius ) , maxRadius , direction , maxDistance , layerMask ) ; if ( sphereCastHits . Length == 0 ) { return Array . Empty < ConeCastHit > ( ) ; } for ( int i = 0 ; i < sphereCastHits . Length ; i ++ ) { Vector3 hitPoint = sphereCastHits [ i ] . point ; Vector3 directionToHit = hitPoint - origin ; float angleToHit = Vector3 . Angle ( direction , directionToHit ) ; if ( angleToHit < coneAngle ) { coneCastHitList . Add ( new ConeCastHit ( sphereCastHits [ i ] , directionToHit . magnitude , angleToHit ) ) ; } } return coneCastHitList . ToArray ( ) ; }
Function casts a sphere along a ray and checks if the hitpoint is within the angle of the cone and returns detailed information.
Function casts a sphere along a ray and checks if the hitpoint is within the angle of the cone and returns detailed information .
An array of structs that contain RaycastHit, distance to hit, and the angle of all the objects that were hit.
An array of structs that contain RaycastHit distance to hit and the angle of all the objects that were hit .
origin
Vector3
The vertex of the cone and the at the start of the sweep.
The vertex of the cone and the at the start of the sweep .
direction
Vector3
The direction into which to sweep the sphere
The direction into which to sweep the sphere
maxRadius
float
The radius of the sweep.
The radius of the sweep .
maxDistance
float
The max length of the cast.
The max length of the cast .
coneAngle
float
The angle used to define the cone.
The angle used to define the cone .
layerMask
LayerMask
A Layer mask that is used to selectively ignore colliders when casting a capsule.
A Layer mask that is used to selectively ignore colliders when casting a capsule .