using UnityEngine;

namespace ScrambleKit.Move
{
    public static class RigidBody2DExtensions
    {
        public static void AccelTowardVelocityByAtMost(this Rigidbody2D rb, Vector2 targetVelocity, float maxAccelPerSec)
        {
            Vector2 deltaVel = targetVelocity - rb.velocity;
            float changePossible = Mathf.Min(deltaVel.magnitude / Time.fixedDeltaTime, maxAccelPerSec);

            Vector2 accel = changePossible * deltaVel.normalized;
            Vector2 forceAccel = rb.mass * accel;
            rb.AddForce(forceAccel);
        }

    }
}