Getting a rigidbody to stick to a moving object

I am trying to get my character to stick to objects when a button is pressed. I have gotten it to work on stationary objects. However, when an object is moving my character will slide around the object and eventually be flung of

The current code I have for sticking to objects is

        if (leftTriggerPressed && CanStick)
        {
            rb.velocity = Vector2.zero; // Stop all current movement

          
            Vector2 stickForce = -surfaceNormal * 10f;
            rb.AddForce(Vector2.ClampMagnitude(stickForce, maxStickForce), ForceMode2D.Impulse);
        }

where CanStick is set to true when my character is colliding with an object it is eligible to stick to

Is there a way to allow my character to stick to a moving object?

You are moveing your player by applying force to it.

Have you tried getting the object velocity at every frame and applying that velocity to your player RB instead?

you might be able to do this by setting the player RB to kinematic temporarily (while sticking).

also try to do this in the fixedUpdate() function.

let us know the result.

Leave a Comment