playMaker

Author Topic: Find Closest - Now with an Ignore Owner option  (Read 10808 times)

Neural Echo

  • 1.2 Beta
  • Playmaker Newbie
  • *
  • Posts: 12
Find Closest - Now with an Ignore Owner option
« 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;
}
}
}

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
Re: Find Closest - Now with an Ignore Owner option
« Reply #1 on: April 06, 2011, 07:47:48 PM »
Nice! Will test this out. Thank you for sharing!

justifun

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 280
Re: Find Closest - Now with an Ignore Owner option
« Reply #2 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?

justifun

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 280
Re: Find Closest - Now with an Ignore Owner option
« Reply #3 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.

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Find Closest - Now with an Ignore Owner option
« Reply #4 on: December 10, 2023, 08:37:58 AM »
Hi.
if its a bugfix only I would suggest to keep the same script name :)

justifun

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 280
Re: Find Closest - Now with an Ignore Owner option
« Reply #5 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.