Skip to content

Commit e87d97d

Browse files
committed
fix: gizmos
1 parent 975ec98 commit e87d97d

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

Assets/Code/VisionCone/Detectors/TargetHalfEllipseDetector.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ public class TargetHalfEllipseDetector : BaseTargetDetector
1010
[BoxGroup("Half Ellipse Detector Settings")] [SerializeField] private Vector3 _centerOffset = new(0f, 0f, -2f);
1111
[BoxGroup("Half Ellipse Detector Settings")] [SerializeField] private float _preLength = 1.5f;
1212
[BoxGroup("Half Ellipse Detector Settings")] [SerializeField] private float _length = 2f;
13-
13+
[BoxGroup("Half Ellipse Detector Settings")] [SerializeField, Min(0f)] private float _raycastOffset = 0.5f;
14+
1415
protected override Enemy FindVisibleTarget()
1516
{
1617
Vector3 origin = _body.position + _body.rotation * _centerOffset;
@@ -30,16 +31,19 @@ protected override Enemy FindVisibleTarget()
3031

3132
if (Physics.Raycast(origin, dir, out RaycastHit hit, distance, _targetMask))
3233
{
34+
float adjustedDistance = Mathf.Max(0f, hit.distance - _raycastOffset);
35+
Vector3 adjustedPoint = origin + dir * adjustedDistance;
36+
37+
if (Physics.Raycast(origin, dir, out RaycastHit blockHit, adjustedDistance, _obstacleMask))
38+
continue;
39+
3340
if (hit.collider.TryGetComponent(out Enemy enemy))
3441
{
35-
if (distance < closestDistance)
42+
float actualDistance = Vector3.Distance(origin, adjustedPoint);
43+
if (actualDistance < closestDistance)
3644
{
37-
if (!Physics.Raycast(origin, dir, out RaycastHit blockHit, distance, _obstacleMask) ||
38-
blockHit.transform == enemy.transform || blockHit.transform.IsChildOf(enemy.transform))
39-
{
40-
closestEnemy = enemy;
41-
closestDistance = distance;
42-
}
45+
closestEnemy = enemy;
46+
closestDistance = actualDistance;
4347
}
4448
}
4549
}
@@ -55,11 +59,10 @@ private void OnDrawGizmosSelected()
5559
return;
5660

5761
Vector3 origin = _body.position + _body.rotation * _centerOffset;
58-
59-
Gizmos.color = Color.yellow;
6062
int segments = Mathf.Max(8, _precision);
6163
Vector3 prevPoint = origin;
6264

65+
Gizmos.color = Color.yellow;
6366
for (int i = 0; i <= segments; i++)
6467
{
6568
float t = i / (float)segments;
@@ -85,13 +88,17 @@ private void OnDrawGizmosSelected()
8588

8689
if (Physics.Raycast(origin, dir, out RaycastHit hit, distance, _obstacleMask | _targetMask))
8790
{
91+
float adjustedDistance = Mathf.Max(0f, hit.distance - _raycastOffset);
92+
Vector3 adjustedPoint = origin + dir * adjustedDistance;
93+
8894
if (hit.collider.TryGetComponent(out Enemy _))
8995
color = Color.yellow;
9096
else
9197
color = Color.red;
9298

9399
Gizmos.color = color;
94-
Gizmos.DrawLine(origin, hit.point);
100+
Gizmos.DrawLine(origin, adjustedPoint);
101+
Gizmos.DrawSphere(adjustedPoint, 0.025f);
95102
}
96103
else
97104
{

Assets/Resources/VisionCone/VisionHalfEllipse.prefab

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ MonoBehaviour:
5555
_coneMaterial: {fileID: 2100000, guid: 17eb323d7fdc02b4aba662823b34aed5, type: 2}
5656
_sortOrder: 1
5757
_precision: 300
58-
_width: 10
58+
_width: 12
5959
_height: 5
6060
_segments: 64
6161
_centerOffset: {x: 0, y: 0, z: 0}
@@ -84,7 +84,7 @@ MonoBehaviour:
8484
_body: {fileID: 6406893710322832938}
8585
_isShowDebug: 1
8686
_precision: 256
87-
_width: 10
87+
_width: 12
8888
_height: 5
8989
_centerOffset: {x: 0, y: 0, z: 0}
9090
_preLength: 10

0 commit comments

Comments
 (0)