playMaker

Author Topic: Get Transform Velocity / Direction , Speed / Magnitude  (Read 3274 times)

Digitom

  • Junior Playmaker
  • **
  • Posts: 61
Get Transform Velocity / Direction , Speed / Magnitude
« on: February 05, 2020, 01:44:39 PM »
Hello,

I created an action that gets the actual velocity of the gameobject's Transform. This is useful for syncing animations that depend on the actual movement of a object versus syncing animations through inputs. Also useful if you move your player through a transform and not the rigidbody. Also threw in a getter for speed / magnitude as well.

« Last Edit: April 06, 2020, 03:38:42 PM by Digitom »

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: Get Transform Velocity / Direction , Speed / Magnitude
« Reply #1 on: February 05, 2020, 06:36:56 PM »
Looks interesting, thank you! :)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get Transform Velocity / Direction , Speed / Magnitude
« Reply #2 on: February 06, 2020, 06:09:56 AM »
Hi,

 cool, Added to the Ecosystem, thanks! I updated it a bit.

Bye,

 Jean

Digitom

  • Junior Playmaker
  • **
  • Posts: 61
Re: Get Transform Velocity / Direction , Speed / Magnitude
« Reply #3 on: February 06, 2020, 09:49:01 AM »
Thanks Jean,

I will cache variables in the future from now on when doing this stuff and follow this other formatting.

Just a heads up you may need to round the sqr magnitude as well or you get some wonky values popping up.

Code: [Select]
void CalculateSpeed()
        {
            _go = Fsm.GetOwnerDefaultTarget(gameObject);
           
            _direction = _go.transform.position - lastPos;
           
            if (!storeSquareMagnitude.IsNone) storeSquareMagnitude.Value = Mathf.Round((_direction / Time.deltaTime).sqrMagnitude * 100f) / 100f;

            if (!storeMagnitude.IsNone) storeMagnitude.Value = Mathf.Round((_direction / Time.deltaTime).magnitude * 100f) / 100f;

            _direction = _direction.normalized;
           
            if (space == Space.Self) _direction = _go.transform.InverseTransformDirection(_direction);
           
            if (!storeDirection.IsNone) storeDirection.Value = _direction;
           
            lastPos = _go.transform.position;
        }

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get Transform Velocity / Direction , Speed / Magnitude
« Reply #4 on: February 07, 2020, 02:19:55 AM »
Hi,

 done :)

Bye,

 Jean

Digitom

  • Junior Playmaker
  • **
  • Posts: 61
Re: Get Transform Velocity / Direction , Speed / Magnitude
« Reply #5 on: April 06, 2020, 03:42:37 PM »
Hello Jean,

Apologies but I had to fix this again. Had to add the initial position in the OnEnter.

Please update on the ecosystem, Thanks!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get Transform Velocity / Direction , Speed / Magnitude
« Reply #6 on: April 09, 2020, 03:55:38 AM »
Hi,

 Can you show your script as it is now?

Bye,

 Jean

Digitom

  • Junior Playmaker
  • **
  • Posts: 61
Re: Get Transform Velocity / Direction , Speed / Magnitude
« Reply #7 on: April 09, 2020, 09:28:37 AM »
sure thing

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2020. All rights reserved.
//Original action: https://hutonggames.com/playmakerforum/index.php?topic=21655.msg95032#msg95032
/*--- __ECO__ __PLAYMAKER__ __ACTION__ ---*/

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Transform)]
    [Tooltip("Gets the world transform velocity and direction of a GameObject")]
    public class GetTransformVelocity : FsmStateAction
    {
        public enum UpdateType { Update, FixedUpdate, LateUpdate}

        [RequiredField]
        [UIHint(UIHint.Variable)]
        public FsmOwnerDefault gameObject;

        [Tooltip("Store the direction / velocity")]
        [UIHint(UIHint.Variable)]
        public FsmVector3 storeDirection;

        [Tooltip("Store local direction or world?")]
        public Space space;

        [Tooltip("Store the magnitude / speed")]
        [UIHint(UIHint.Variable)]
        public FsmFloat storeMagnitude;
       
        [Tooltip("Store the square magnitude / speed. Takes less performances")]
        [UIHint(UIHint.Variable)]
        public FsmFloat storeSquareMagnitude;
       
        public UpdateType updateType;
       
        private Vector3 _lastPos;
        private Vector3 _direction;
        private GameObject _go;

        public override void Reset()
        {
            gameObject = null;
            storeDirection = null;
            storeMagnitude = null;
            storeSquareMagnitude = null;
        }

        public override void OnPreprocess()
        {
            if (updateType == UpdateType.FixedUpdate)
                Fsm.HandleFixedUpdate = true;
            else if (updateType == UpdateType.LateUpdate)
                Fsm.HandleLateUpdate = true;
        }

        public override void OnEnter()
        {
            _go = Fsm.GetOwnerDefaultTarget(gameObject);
            _lastPos = _go.transform.position;
        }

        public override void OnUpdate()
        {
            if (updateType == UpdateType.Update)
                CalculateSpeed();
        }

        public override void OnFixedUpdate()
        {
            if (updateType == UpdateType.FixedUpdate)
                CalculateSpeed();
        }

        public override void OnLateUpdate()
        {
            if (updateType == UpdateType.LateUpdate)
                CalculateSpeed();
        }

        void CalculateSpeed()
        {
           
            _direction = _go.transform.position - _lastPos;
           
            if (!storeSquareMagnitude.IsNone) storeSquareMagnitude.Value = Mathf.Round((_direction / Time.deltaTime).sqrMagnitude * 100f) / 100f;

            if (!storeMagnitude.IsNone) storeMagnitude.Value = Mathf.Round((_direction / Time.deltaTime).magnitude * 100f) / 100f;

            _direction = _direction.normalized;
           
            if (space == Space.Self) _direction = _go.transform.InverseTransformDirection(_direction);
           
            if (!storeDirection.IsNone) storeDirection.Value = _direction;
           
            _lastPos = _go.transform.position;
        }

    }
}

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get Transform Velocity / Direction , Speed / Magnitude
« Reply #8 on: April 14, 2020, 01:55:47 AM »
Hi,

 thanks, updated.

Bye,

 Jean