Join our Discord Channel
// License: Attribution 4.0 International (CC BY 4.0)/*--- __ECO__ __PLAYMAKER__ __ACTION__ ---*/// Keywords: Array Closest Vector 3using UnityEngine;using System;using System.Collections;namespace HutongGames.PlayMaker.Actions{ [ActionCategory("ArrayMaker/ArrayList")] [Tooltip("Return the closest Vector3 within an arrayList from a position.")] public class ArrayListGetClosestVector3 : ArrayListActions { [ActionSection("Set up")] [RequiredField] [Tooltip("The gameObject with the PlayMaker ArrayList Proxy component")] [CheckForComponent(typeof(PlayMakerArrayListProxy))] public FsmOwnerDefault gameObject; [Tooltip("Author defined Reference of the PlayMaker ArrayList Proxy component ( necessary if several component coexists on the same GameObject")] public FsmString reference; [Tooltip("Compare the distance of the items in the list to the position of this Vector3")] public FsmVector3 distanceFrom; public FsmBool greaterThanDistanceFrom; public bool everyframe; [ActionSection("Result")] [UIHint(UIHint.Variable)] public FsmVector3 closestVector3; [UIHint(UIHint.Variable)] public FsmInt closestIndex; private int _index; public override void Reset() { gameObject = null; reference = null; distanceFrom = null; closestIndex = null; everyframe = true; greaterThanDistanceFrom = false; } public override void OnEnter() { if (! SetUpArrayListProxyPointer(Fsm.GetOwnerDefaultTarget(gameObject),reference.Value) ) { Finish(); } DoAction(); if (!everyframe) { Finish(); } } public override void OnUpdate() { DoAction(); } void DoAction() { if (! isProxyValid()) { return; } Vector3 root = distanceFrom.Value; float sqrDist = Mathf.Infinity; float sqrDistTest; foreach(Vector3 _v3 in proxy.arrayList) { sqrDistTest = (_v3 - root).sqrMagnitude; if (greaterThanDistanceFrom.Value) { { if (sqrDistTest <= sqrDist & root != _v3) { sqrDist = sqrDistTest; closestVector3.Value = _v3; closestIndex.Value = _index; } } } else { if (sqrDistTest<= sqrDist) { sqrDist = sqrDistTest; closestVector3.Value = _v3; closestIndex.Value = _index; } } } _index++; } } }