Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: lk.xxxx on November 04, 2023, 10:15:58 AM

Title: How do I get linear velocity for a point, not for center of mass?
Post by: lk.xxxx on November 04, 2023, 10:15:58 AM
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!
Title: Re: How do I get linear velocity for a point, not for center of mass?
Post by: lk.xxxx 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;
        }
    }
}
Title: Re: How do I get linear velocity for a point, not for center of mass?
Post by: djaydino on November 06, 2023, 07:38:50 AM
Hi.
Have you tried looking in the Ecosystem (https://hutonggames.fogbugz.com/default.asp?W1181)

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 :)