Weird Rigidbody behaviour

Hello!

The player controller in my game uses a rigidbody that moves around with AddForce. There is also a “snapping” mechanic where the rigidbody gets moved from A to B over time. In order for it to work smoothly I change the rigidbody to a kinematic during the snapping movement, otherwise it’s all jittery. Anyway, that works fine except for when under one specific circumstance.

If I’m “mid snap” and the player moves, I stop the snapping. But next time I go to snap the rigidbody somewhere it moves and jiggles a bit in the direction of the Input at the time the previous snap was cancelled, almost like it is saving velocity from the last time it was in Kinematic state (which is weird, because to my understanding kinematic rb should not have physics velocity). I’m not sure if this is an expected behavior, but any advice or explanations would be great. Relevant code and examples below

// Set snap position
public void SnapToCenterPointOfSelectedObject()
{
     _snapPosition = placedObject.GetCenterPointOfPlacedObject();
     _rigidbody.isKinematic = true;
     _snappingToPosition = true;
}


// Code to stop snapping if input is detected mid snap
private void Update()
{
     if (_snappingToPosition)
     {
         if (_playerInput.MoveInput != Vector2.zero)
         {
             _snappingToPosition = false;
             _rigidbody.isKinematic = false;
         }
     }
}


// Snapping code
private void FixedUpdate()
{
    if (_snappingToPosition)
    {
        if (!_rigidbody.isKinematic) _rigidbody.isKinematic = true;

       _rigidbody.MovePosition(Vector3.SmoothDamp(_transform.position, _snapPosition, ref _snapVelRef, _snapSpeed, Mathf.Infinity, Time.fixedDeltaTime));

        if (GeneralUtility.Approximately(_transform.position, _snapPosition, 0.25f))
        {
            _snappingToPosition = false;
            _rigidbody.isKinematic = false;
        }
    }
}

Working Regular Snap
9754021--1396306--Recording 2024-04-06 at 17.14.05.gif

Snap cancelled midway through by player input. You can see the weird little movement I’m referring to during the second snap almost like it’s pushing forward and to the right before moving toward the object.
9754021--1396309--Recording 2024-04-06 at 17.15.47.gif

Alright I figured it out. This is happening because I’m using SmoothDamp.

The velocity reference “_snapVelRef” needs to be set back to Vector3.zero after I stop the movement otherwise once it starts snapping again the velocity is going to be elevated from when it was stopped mid snap.