playMaker

Author Topic: Get Transform Distance  (Read 5056 times)

sebaslive

  • Hero Member
  • *****
  • Posts: 572
    • Frame Tale Studios
Get Transform Distance
« 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;
               
            }

        }
    }
}
All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5


Follow me @sebasRez

mdotstrange

  • Hero Member
  • *****
  • Posts: 555
    • Can't code? Who cares! Make games anyway!
Re: Get Transform Distance
« Reply #1 on: August 26, 2015, 11:41:55 PM »
Awesome, thanks!  :)
Indie game dev and instructor at the Strange School. Learn Playmaker at the Strange School!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get Transform Distance
« Reply #2 on: August 27, 2015, 08:37:10 AM »
Hi,

 Great. Don't forget there is a GetDistance2 custom action as well, but your action goes further, so it's a nice addition.

 I have made it available on the Ecosystem.

Bye,

 Jean

sebaslive

  • Hero Member
  • *****
  • Posts: 572
    • Frame Tale Studios
Re: Get Transform Distance
« Reply #3 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
All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5


Follow me @sebasRez

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get Transform Distance
« Reply #4 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

sebaslive

  • Hero Member
  • *****
  • Posts: 572
    • Frame Tale Studios
Re: Get Transform Distance
« Reply #5 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.
All my VR games use Steam VR Playmaker Toolkit: http://u3d.as/u20
And Oculus Touch Playmaker Toolkit: http://u3d.as/QT5


Follow me @sebasRez

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get Transform Distance
« Reply #6 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, so simply clone this on your computer, edit the script and push back and you are done :)

 Bye,

 Jean