-
Notifications
You must be signed in to change notification settings - Fork 12
/
ConeCastExtension.cs
32 lines (27 loc) · 1.23 KB
/
ConeCastExtension.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System.Collections.Generic;
using UnityEngine;
public static class ConeCastExtension
{
public static RaycastHit[] ConeCastAll(this Physics physics, Vector3 origin, float maxRadius, Vector3 direction, float maxDistance, float coneAngle)
{
RaycastHit[] sphereCastHits = Physics.SphereCastAll(origin - new Vector3(0,0,maxRadius), maxRadius, direction, maxDistance);
List<RaycastHit> coneCastHitList = new List<RaycastHit>();
if (sphereCastHits.Length > 0)
{
for (int i = 0; i < sphereCastHits.Length; i++)
{
sphereCastHits[i].collider.gameObject.GetComponent<Renderer>().material.color = new Color(1f, 1f, 1f);
Vector3 hitPoint = sphereCastHits[i].point;
Vector3 directionToHit = hitPoint - origin;
float angleToHit = Vector3.Angle(direction, directionToHit);
if (angleToHit < coneAngle)
{
coneCastHitList.Add(sphereCastHits[i]);
}
}
}
RaycastHit[] coneCastHits = new RaycastHit[coneCastHitList.Count];
coneCastHits = coneCastHitList.ToArray();
return coneCastHits;
}
}