playMaker

Author Topic: How do I get linear velocity for a point, not for center of mass?  (Read 332 times)

lk.xxxx

  • Playmaker Newbie
  • *
  • Posts: 2
Hello!

How do I get linear velocity for a point, not for center of mass?
In Unity there is RigidBody.GetPointVelocity function.
Is there something similar in playmaker?

Thanks!

lk.xxxx

  • Playmaker Newbie
  • *
  • Posts: 2
Re: How do I get linear velocity for a point, not for center of mass?
« Reply #1 on: November 05, 2023, 05:20:33 AM »
Using ChatGPT I got the following code:

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
    [ActionCategory(ActionCategory.Physics)]
    [Tooltip("Gets the linear velocity of a specific point in a Rigidbody every frame.")]
    public class GetPointVelocity : FsmStateAction
    {
        [RequiredField]
        [CheckForComponent(typeof(Rigidbody))]
        [Tooltip("The GameObject with the Rigidbody.")]
        public FsmOwnerDefault gameObject;

        [Tooltip("The point in world space.")]
        public FsmVector3 point;

        [UIHint(UIHint.Variable)]
        [Tooltip("Store the velocity in a Vector3 Variable.")]
        public FsmVector3 storeResult;

        private Rigidbody rigidBody;

        public override void Reset()
        {
            gameObject = null;
            point = null;
            storeResult = null;
        }

        public override void OnEnter()
        {
            var go = Fsm.GetOwnerDefaultTarget(gameObject);
            if (go == null)
            {
                Finish();
                return;
            }

            rigidBody = go.GetComponent<Rigidbody>();

            if (rigidBody == null)
            {
                Finish();
                return;
            }

            // Execute every frame
            Fsm.HandleLateUpdate = true;
        }

        public override void OnUpdate()
        {
            DoGetPointVelocity();
        }

        private void DoGetPointVelocity()
        {
            if (rigidBody != null)
            {
                Vector3 velocity = rigidBody.GetPointVelocity(point.Value);
                storeResult.Value = velocity;
            }
        }

        public override void OnExit()
        {
            // Reset handle late update on exit
            Fsm.HandleLateUpdate = false;
        }
    }
}

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: How do I get linear velocity for a point, not for center of mass?
« Reply #2 on: November 06, 2023, 07:38:50 AM »
Hi.
Have you tried looking in the Ecosystem

The ChatGPT code looks correct, but be careful with using ChatGPT, as it is by far perfect.
If you have a little knowledge of C# most of the times you can see if it made mistakes or bloated the script.

if you have no C# knowledge, I would suggest to join discord so you can post any code you created to validate by others :)