forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindNearest.cs
More file actions
27 lines (24 loc) · 857 Bytes
/
Copy pathFindNearest.cs
File metadata and controls
27 lines (24 loc) · 857 Bytes
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
using UnityEngine;
public class FindNearest : MonoBehaviour
{
private Vector3 nearestTargetPosition;
public void Update()
{
// Find nearest Target.
// When comparing distances, it's cheaper to compare
// the squares of the distances because doing so
// avoids computing square roots.
float nearestDistSq = float.MaxValue;
foreach (var targetTransform in Spawner.TargetTransforms)
{
Vector3 offset = targetTransform.localPosition - transform.localPosition;
float distSq = offset.sqrMagnitude;
if (distSq < nearestDistSq)
{
nearestDistSq = distSq;
nearestTargetPosition = targetTransform.localPosition;
}
}
Debug.DrawLine(transform.localPosition, nearestTargetPosition);
}
}