forked from mdotstrange/CustomBehaviorDesignerTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetClosestGameObjectFromList.cs
86 lines (67 loc) · 2.6 KB
/
getClosestGameObjectFromList.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using UnityEngine;
using System.Collections.Generic;
namespace BehaviorDesigner.Runtime.Tasks.Basic.SharedVariables
{
[TaskCategory("Basic/SharedVariable")]
[TaskDescription("Gets the closest game object in a list.")]
public class GetClosestGameObjectFromList : Action
{
[RequiredField]
[Tooltip("The SharedVector3List to use")]
public SharedGameObjectList storedGameObjectList;
public SharedGameObject distanceFromGO;
public SharedVector3 distanceFrom;
public SharedGameObject closestGameObject;
public SharedInt closestIndex;
private Vector3 positionToTest;
public SharedFloat distance;
private SharedVector3 closestVector3;
public override void OnAwake()
{
}
public override TaskStatus OnUpdate()
{
//Debug.Log("DistanceFromGO value " + distanceFromGO.Value);
if (distanceFromGO.Value != null)
{
positionToTest = distanceFromGO.Value.gameObject.transform.position;
} else
{
positionToTest = distanceFrom.Value;
}
float sqrDist = Mathf.Infinity;
int _index = 0;
float sqrDistTest;
//foreach (GameObject singleGO in storedGameObjectList.Value)
for(int index = 0; index < storedGameObjectList.Value.Count; index++)
{
var singleGO = storedGameObjectList.Value[index];
Vector3 singleVector = singleGO.transform.position;
if (singleVector != null)
{
sqrDistTest = (singleVector - positionToTest).sqrMagnitude;
if (sqrDistTest <= sqrDist)
{
sqrDist = sqrDistTest;
closestVector3 = singleVector;
closestIndex.Value = _index;
}
}
_index++;
}
distance.Value = Vector3.Distance(positionToTest, closestVector3.Value);
closestGameObject.Value = storedGameObjectList.Value[closestIndex.Value];
return TaskStatus.Success;
}
public override void OnReset()
{
storedGameObjectList = null;
distanceFrom = Vector3.zero;
closestGameObject = null;
closestIndex = null;
positionToTest = Vector3.zero;
distanceFromGO = null;
distance = null;
}
}
}