playMaker

Author Topic: get distance with axis ignore flags  (Read 2851 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14210
  • Official Playmaker Support
    • View Profile
get distance with axis ignore flags
« on: January 02, 2012, 01:25:03 AM »
Hi,

 following Withthewicked request http://hutonggames.com/playmakerforum/index.php?topic=974.msg4006#msg4006

 Here is a more flexible version of GetDistance action that accepts flags to ignore x y or z axis ( workd coordinates reference).

 With these flags you can obtain the following information:

if x axis is ignored only: you get the distance on the world YZ plane

if Y and Z axis are ignored, you get the distance on the world x axis

 You can then deduce the different combinations of flags from this. warning, ignoring all axis obvioulsy will give you a distance of 0...

possible improvements: instead of using global axis, we could have this action using the ownergameObject for the axis reference thus giving us the ability to get for the example the "x" distance FROM the gameObject coordinates not from a world coordinate point of view.

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

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.GameObject)]
[Tooltip("Measures the Distance betweens 2 Game Objects and stores the result in a Float Variable. \nOptionaly ignore axis to obtain planar distance axis distance. \nExample: ignore the y axis only to obtain the distance on the YZ plane. \nExample: ignore Y and Z to obtain the distance on the x axis.")]
public class GetWorldDistance : FsmStateAction
{
[RequiredField]
[Tooltip("The Game Object to work with")]
public FsmOwnerDefault gameObject;

[RequiredField]
[Tooltip("The target Game Object to work with")]
public FsmGameObject target;

[RequiredField]
[Tooltip("Store the distance between the gameObject and the target")]
[UIHint(UIHint.Variable)]
public FsmFloat storeResult;

[Tooltip("The world x axis is ignored")]
public bool ignoreX;

[Tooltip("The world y axis is ignored")]
public bool ignoreY;

[Tooltip("The world z axis is ignored")]
public bool ignoreZ;


public bool everyFrame;


public override void Reset()
{
gameObject = null;
target = null;
storeResult = null;

ignoreX = false;
ignoreY = false;
ignoreZ = false;

everyFrame = true;
}

public override void OnEnter()
{
DoGetDistance();

if (!everyFrame)
Finish();
}
public override void OnUpdate()
{
DoGetDistance();
}

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

if (go == null || target.Value == null || storeResult == null)
return;



if (!ignoreX && !ignoreY && ! ignoreZ)
{
storeResult.Value = Vector3.Distance(go.transform.position, target.Value.transform.position);
}else{

Vector3 vector = target.Value.transform.position - go.transform.position;
if (ignoreX)
{
vector.x = 0;
}
if (ignoreY)
{
vector.y = 0;
}
if(ignoreZ)
{
vector.z = 0;
}

storeResult.Value = vector.magnitude;
}

}

}
}

Comment and questions welcome :)

 Bye,

 Jean

Andrew.Lukasik

  • Full Member
  • ***
  • Posts: 134
    • View Profile
    • my twitter @andrewlukasik
Re: get distance with axis ignore flags
« Reply #1 on: January 02, 2012, 10:06:22 AM »
Nice. I'm sure this will help me with something very soon.
Thanks Jeanfabre!

tobbeo

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 186
    • View Profile
Re: get distance with axis ignore flags
« Reply #2 on: January 02, 2012, 10:49:17 PM »
I like this one a lot too, thanks Jean!

WitchTheWicked

  • Playmaker Newbie
  • *
  • Posts: 8
    • View Profile
Re: get distance with axis ignore flags
« Reply #3 on: January 06, 2012, 11:58:32 PM »
I love this alot!