Playmaker Forum

PlayMaker Updates & Downloads => Share New Actions => Topic started by: sebaslive on August 26, 2015, 03:18:53 PM

Title: Get Transform Distance
Post by: sebaslive on August 26, 2015, 03:18:53 PM
Really useful script for getting the distance between two objects in either its Vector 3, X, Y, or Z

You can also get the negative distance if the object is behind or in front of the target. I use it a ton so get it while its hot!

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

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.GameObject)]
    [Tooltip("Gets the distance based on the Transform Values.")]
    public class GetTransformDistance : FsmStateAction
    {

        [RequiredField]
        public FsmOwnerDefault gameObject;
        [RequiredField]
        public FsmGameObject target;


        public enum distanceSelect
        {
            Vector3Distance,
            xDistance,
            yDistance,
            zDistance,
        };
        public distanceSelect selectDistanceType;
        [Tooltip("Gets a negative and positive values depending on the transform position.")]
        public FsmBool getNegative;
        [RequiredField]
        [UIHint(UIHint.Variable)]
        public FsmFloat storeResult;
        public bool everyFrame;

        public override void Reset()
        {
            gameObject = null;
            target = null;
            getNegative = null;
            storeResult = null;
            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;
            switch (selectDistanceType)
            {
                case distanceSelect.Vector3Distance:
                    if (getNegative.Value == true)
                    {
                        var position = go.transform.position;
                        var newPos = target.Value.transform.TransformPoint(position);
                        storeResult.Value = (newPos.x + newPos.y + newPos.z);
                    }
                    else
                    {

                        storeResult.Value = Vector3.Distance(go.transform.position, target.Value.transform.position);
                    }
                    break;

                case distanceSelect.xDistance:
                    if (getNegative.Value == true)
                    {
                        var position = go.transform.position;
                        var newX = target.Value.transform.TransformPoint(position);
                        storeResult.Value = newX.x;
                    }
                    else
                    {
                        storeResult.Value = Mathf.Abs(go.transform.position.x - target.Value.transform.position.x);
                    }
                    break;

                case distanceSelect.yDistance:
                    if (getNegative.Value == true)
                    {
                        var position = go.transform.position;
                        var newY = target.Value.transform.TransformPoint(position);
                        storeResult.Value = newY.y;
                    }
                    else
                    {
                        storeResult.Value = Mathf.Abs(go.transform.position.y - target.Value.transform.position.y);
                    }
                    break;

                case distanceSelect.zDistance:
                    if (getNegative.Value == true)
                    {
                        var position = go.transform.position;
                        var newZ = target.Value.transform.TransformPoint(position);
                        storeResult.Value = newZ.z;
                    }
                    else
                    {
                        storeResult.Value = Mathf.Abs(go.transform.position.z - target.Value.transform.position.z);
                    }
                    break;
               
            }

        }
    }
}
Title: Re: Get Transform Distance
Post by: mdotstrange on August 26, 2015, 11:41:55 PM
Awesome, thanks!  :)
Title: Re: Get Transform Distance
Post by: jeanfabre on August 27, 2015, 08:37:10 AM
Hi,

 Great. Don't forget there is a GetDistance2 (https://github.com/jeanfabre/PlayMakerCustomActions_U3/blob/master/Assets/PlayMaker%20Custom%20Actions/GameObject/GetDistance2.cs) custom action as well, but your action goes further, so it's a nice addition.

 I have made it available on the Ecosystem (https://hutonggames.fogbugz.com/default.asp?W1181).

Bye,

 Jean
Title: Re: Get Transform Distance
Post by: sebaslive on August 27, 2015, 08:58:43 AM
Hey Jean, thanks! This is always my worry that there is an action that already does what I plan on making but I don't know the name of it :P
Title: Re: Get Transform Distance
Post by: jeanfabre on August 27, 2015, 09:22:38 AM
Hi,

 no worries! your action brings more on the table, it's totally fine, and even if it did not, we all get that, I sometime write an action twice because I forgot I did it before... It's a problem that I am not sure how to solve, apart from searching on the forum and ecosystem prior doing it, but then depending on the wording, it can be missed...

any idea on how to solve this is welcome :)


Bye,

 Jean
Title: Re: Get Transform Distance
Post by: sebaslive on August 27, 2015, 01:51:57 PM
Hey Jean, can I have permission to change my scripts on Ecosystem?

There was a hi jink with this one that needs fixing.

Please everyone use this updated version which has the proper settings for the Negative values.
Title: Re: Get Transform Distance
Post by: jeanfabre on August 27, 2015, 02:31:31 PM
Hi,

 Changes is up on the Ecosystem.

 Actually, for Unity 4 + actions, the Ecosystem feeds from a Github public repository (https://github.com/jeanfabre/PlayMakerCustomActions_U4), so simply clone this on your computer, edit the script and push back and you are done :)

 Bye,

 Jean