1
This commit is contained in:
@@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using System.Linq;
|
||||
using Cysharp.Threading.Tasks;
|
||||
using Unity.Mathematics;
|
||||
using UnityEngine.Jobs;
|
||||
using UnityEngine.Pool;
|
||||
using UnityEngine.Profiling;
|
||||
@@ -13,19 +14,23 @@ namespace BITKit.Sensors
|
||||
{
|
||||
public class RangeSensor : Sensor
|
||||
{
|
||||
[Header(Constant.Header.Settings)] public float radius;
|
||||
public int fov;
|
||||
public bool requireSight;
|
||||
|
||||
[Header(Constant.Header.Settings)] public LayerMask blockLayer;
|
||||
|
||||
[Header(Constant.Header.Debug)] [Header(Constant.Header.InternalVariables)]
|
||||
private FrameUpdate frameUpdater;
|
||||
[Header(Constant.Header.Settings)]
|
||||
[SerializeField] private float radius;
|
||||
[SerializeField] private float detectedHeightWeight=0.5f;
|
||||
[Header(Constant.Header.Optional)]
|
||||
[SerializeField] private Optional<int> fov;
|
||||
[SerializeField] private Optional<LayerMask> requireSight;
|
||||
[SerializeField] private Optional<float> perceptionRadius;
|
||||
|
||||
public float DetectedHeightWeight
|
||||
{
|
||||
get=>detectedHeightWeight;
|
||||
set=>detectedHeightWeight=value;
|
||||
}
|
||||
private readonly Collider[] colliders = new Collider[32];
|
||||
private RaycastHit[] hits = new RaycastHit[32];
|
||||
private readonly HashSet<int> tempHashSet = new();
|
||||
|
||||
private readonly RaycastHit[] hits = new RaycastHit[32];
|
||||
private float _delta;
|
||||
|
||||
public override IEnumerable<Transform> Get()
|
||||
{
|
||||
if (!_detectedDoubleBuffer.TryGetRelease(out var newRelease)) return _detectedBuffer;
|
||||
@@ -40,7 +45,7 @@ namespace BITKit.Sensors
|
||||
|
||||
public override UniTask Execute(float delta)
|
||||
{
|
||||
tempHashSet.Clear();
|
||||
_delta = delta;
|
||||
var length = Physics.OverlapSphereNonAlloc(Transform.position, radius, colliders, detectLayer);
|
||||
Profiler.BeginSample("Filter Detected Colliders");
|
||||
var _newDetected = from x in colliders.Take(length) where IsValid(x) select x.transform;
|
||||
@@ -53,10 +58,21 @@ namespace BITKit.Sensors
|
||||
{
|
||||
switch (_collider)
|
||||
{
|
||||
case var _ when Ignores.Contains(_collider.GetInstanceID()):
|
||||
case var _ when ignoreColliders.Contains(_collider):
|
||||
return false;
|
||||
case var _ when fov > 0 && CheckFov(ref _collider) is false:
|
||||
case var _ when requireSight && CheckSight(ref _collider) is false:
|
||||
}
|
||||
|
||||
if (perceptionRadius.Allow)
|
||||
{
|
||||
var distance = Vector3.Distance(Transform.position, _collider.bounds.center);
|
||||
if(distance<=perceptionRadius.Value) return !requireSight.Allow || CheckSight(ref _collider);
|
||||
}
|
||||
switch (_collider)
|
||||
{
|
||||
|
||||
case var _ when fov.Allow && CheckFov(ref _collider) is false:
|
||||
case var _ when requireSight.Allow && CheckSight(ref _collider) is false:
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
@@ -75,23 +91,21 @@ namespace BITKit.Sensors
|
||||
|
||||
private bool CheckSight(ref Collider _collider)
|
||||
{
|
||||
if (!requireSight) return false;
|
||||
var position = _collider.bounds.center;
|
||||
//是检测bounds的顶部
|
||||
position.y += _collider.bounds.extents.y;
|
||||
var location = new Location(Transform);
|
||||
var bounds = _collider.bounds;
|
||||
var position = bounds.center;
|
||||
position.y += bounds.size.y * detectedHeightWeight /2;
|
||||
var selfPosition = Transform.position;
|
||||
var length = Physics.RaycastNonAlloc(
|
||||
location.position,
|
||||
position - location,
|
||||
selfPosition,
|
||||
position - selfPosition,
|
||||
hits,
|
||||
Vector3.Distance(location, position),
|
||||
blockLayer
|
||||
Vector3.Distance(selfPosition, position),
|
||||
requireSight.Value
|
||||
);
|
||||
|
||||
switch (length)
|
||||
{
|
||||
case 0:
|
||||
Debug.DrawLine(location, position, Color.green, 1);
|
||||
Debug.DrawLine(selfPosition, position, Color.green,_delta);
|
||||
return true;
|
||||
case 1:
|
||||
return hits[0].collider == _collider;
|
||||
@@ -107,7 +121,8 @@ namespace BITKit.Sensors
|
||||
var result = ignoreColliders.Contains(x.collider) is false && x.collider != collider1;
|
||||
if (result)
|
||||
{
|
||||
Debug.DrawLine(location, x.point, Color.red, 1);
|
||||
Debug.DrawLine(selfPosition, x.point, Color.yellow,_delta);
|
||||
Debug.DrawLine(selfPosition, position, Color.red,_delta);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
Reference in New Issue
Block a user