PlayMaker Updates & Downloads > Share New Actions

Find Closest - Now with an Ignore Owner option

(1/2) > >>

Neural Echo:
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: ---// (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;
}
}
}

--- End code ---

tobbeo:
Nice! Will test this out. Thank you for sharing!

justifun:
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?

justifun:
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.

djaydino:
Hi.
if its a bugfix only I would suggest to keep the same script name :)

Navigation

[0] Message Index

[#] Next page

Go to full version