22 lines
1.0 KiB
C#
22 lines
1.0 KiB
C#
|
|
using System.Collections.Generic;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
public static class RaycastExtensions {
|
|||
|
|
public static RaycastHit[] ConeCastAll(Vector3 origin, Vector3 direction, float coneAngle, float maxRadius, float maxDistance = Mathf.Infinity, int layerMask = Physics.DefaultRaycastLayers){
|
|||
|
|
var sphereCastHits = Physics.SphereCastAll(origin - new Vector3(0,0,maxRadius), maxRadius, direction, maxDistance);
|
|||
|
|
if(sphereCastHits.Length > 0){
|
|||
|
|
var coneCastHitList = new List<RaycastHit>();
|
|||
|
|
for(int i = 0; i < sphereCastHits.Length; ++i){
|
|||
|
|
var hitPoint = sphereCastHits[i].point;
|
|||
|
|
var directionToHit = hitPoint - origin;
|
|||
|
|
var angleToHit = Vector3.Angle(direction, directionToHit);
|
|||
|
|
if(angleToHit < coneAngle){
|
|||
|
|
coneCastHitList.Add(sphereCastHits[i]);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return coneCastHitList.ToArray();
|
|||
|
|
}else{
|
|||
|
|
return new RaycastHit[0];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|