playMaker

Author Topic: ArrayListGetClosestVector3  (Read 2378 times)

cwmanley

  • Full Member
  • ***
  • Posts: 107
ArrayListGetClosestVector3
« on: January 18, 2016, 01:37:31 PM »
On the Ecosystem or https://snipt.net/cwmanley/ //TODO


Code: [Select]
// License: Attribution 4.0 International (CC BY 4.0)
/*--- __ECO__ __PLAYMAKER__ __ACTION__ ---*/
// Keywords: Array Closest Vector 3

using 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++;
}

}

}
« Last Edit: February 15, 2017, 02:35:00 PM by cwmanley »

dudebxl

  • Hero Member
  • *****
  • Posts: 602
Re: ArrayListGetClosestVector3
« Reply #1 on: January 18, 2016, 08:37:19 PM »
Cool.. thanks dude  :D

600

  • Beta Group
  • Hero Member
  • *
  • Posts: 674
    • Flashing Lights
Re: ArrayListGetClosestVector3
« Reply #2 on: July 24, 2021, 11:07:46 AM »
Hi sorry for necro post, but this is nice action and missing on Ecosystem.
Only thing that needs edit is the index of found closest vector in array list always is 0.

Thanks for making this!