# playMaker

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

#### Digitom

• Playmaker Newbie
• Posts: 47
##### Get Transform Velocity / Direction , Speed / Magnitude
« on: February 05, 2020, 11:44:39 AM »
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, 12:38:42 PM by Digitom »

#### Thore

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

#### jeanfabre

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

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

Bye,

Jean

#### Digitom

• Playmaker Newbie
• Posts: 47
##### Re: Get Transform Velocity / Direction , Speed / Magnitude
« Reply #3 on: February 06, 2020, 07: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

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

done

Bye,

Jean

#### Digitom

• Playmaker Newbie
• Posts: 47
##### Re: Get Transform Velocity / Direction , Speed / Magnitude
« Reply #5 on: April 06, 2020, 12: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

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

Can you show your script as it is now?

Bye,

Jean

#### Digitom

• Playmaker Newbie
• Posts: 47
##### Re: Get Transform Velocity / Direction , Speed / Magnitude
« Reply #7 on: April 09, 2020, 06: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

• Hero Member
• Posts: 15224
• Official Playmaker Support
##### Re: Get Transform Velocity / Direction , Speed / Magnitude
« Reply #8 on: April 13, 2020, 10:55:47 PM »
Hi,

thanks, updated.

Bye,

Jean