Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: Neural Echo on April 06, 2011, 05:21:23 PM

Title: Find Closest - Now with an Ignore Owner option
Post by: Neural Echo on April 06, 2011, 05:21:23 PM
Here's a slightly modified version of the built-in Find Closest action that optionally allows the user to ignore the object that owns the FSM that contains this action.

To update the built-in action with this version, replace the code inside the Assets/PlayMaker/Actions/FindClosest.cs script with the code below.


Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.GameObject)]
[Tooltip("Finds the closest object to the specified Game Object.\nOptionally filter by Tag and Visibility.")]
public class FindClosest : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;
[RequiredField]
[UIHint(UIHint.Tag)]
public FsmString withTag;
[Tooltip("If checked, ignores the object that owns this FSM.")]
public bool ignoreOwner;
public FsmBool mustBeVisible;
[UIHint(UIHint.Variable)]
public FsmGameObject storeObject;
[UIHint(UIHint.Variable)]
public FsmFloat storeDistance;
public bool everyFrame;


public override void Reset()
{
gameObject = null;
withTag = "Untagged";
mustBeVisible = false;
storeObject = null;
}

public override void OnEnter()
{
DoFindClosest();

if (!everyFrame)
Finish();
}

public override void OnUpdate()
{
DoFindClosest();
}

void DoFindClosest()
{
GameObject go = gameObject.OwnerOption == OwnerDefaultOption.UseOwner ? Owner : gameObject.GameObject.Value;

GameObject[] objects = GameObject.FindGameObjectsWithTag(withTag.Value);
GameObject closestObj = null;
var closestDist = Mathf.Infinity;

foreach (var obj in objects)
{
if (ignoreOwner && obj == Owner)
continue;

if (mustBeVisible.Value && !ActionHelpers.IsVisible(obj))
continue;

var dist = (go.transform.position - obj.transform.position).sqrMagnitude;
if (dist < closestDist)
{
closestDist = dist;
closestObj = obj;
}
}

storeObject.Value = closestObj;
storeDistance.Value = closestDist;
}
}
}
Title: Re: Find Closest - Now with an Ignore Owner option
Post by: tobbeo on April 06, 2011, 07:47:48 PM
Nice! Will test this out. Thank you for sharing!
Title: Re: Find Closest - Now with an Ignore Owner option
Post by: justifun on December 09, 2023, 07:31:05 PM
For some reason, the ignore owner feature seems to still target itself.  Could it be because there's been some sort of update to playmaker since this was released?
Title: Re: Find Closest - Now with an Ignore Owner option
Post by: justifun on December 09, 2023, 08:11:22 PM
I'm attaching a new version of Find Closest 2 because I fixed a bug in the "Ignore Owner" check which would still pick the owner even if that was checked.
Title: Re: Find Closest - Now with an Ignore Owner option
Post by: djaydino on December 10, 2023, 08:37:58 AM
Hi.
if its a bugfix only I would suggest to keep the same script name :)
Title: Re: Find Closest - Now with an Ignore Owner option
Post by: justifun on December 10, 2023, 12:42:27 PM
There are 2 different "Find Closest and Find Closest 2" on the ecosystem already.  I am not the original author of either, I just patched 2 and post it here in case anyone runs across a similar problem.