playMaker

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

Digitom

  • Playmaker Newbie
  • *
  • Posts: 37
    • View Profile
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.


Thore

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

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14938
  • Official Playmaker Support
    • View Profile
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: 37
    • View Profile
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

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

 done :)

Bye,

 Jean