How to apply resulting velocity from Rigidbody2d.Slide() to a velocity variable tracked in script

Hi, I have platformer controller which is a Kinematic Rigidbody2D which I calculate the desired velocity in a script and then use RigidBody2d.Slide() to move the body and handle collisions
An issue I’m having is the feedback from the slide movement isn’t applied to the velocity. For example, if the player is holding left/right pushing into a wall, their x velocity will accelerate to their max speed, despite not being applied, and maintained like that until they stop holding left/right, but what I want to happen is the x velocity set to 0 if no horizontal movement is able to be applied. Is it possible to implement this using the SlideResults? What would the correct approach be

Slide has nothing to do with the Rigidbody2D.velocity so I don’t follow what you’re asking here.

You don’t use velocity on a Rigidbody2D which is its movement then also perform a Slide to also cause movement. That’s completely conflicting.

as I said have my own velocity variable in a script which is what I pass into Slide()

You can always manually calculate what a ridbody’s velocity is by comparing its position from the last frame to the position from the current frame.

1 Like

You only said you “calculate the desired velocity in a script” then go on to say “the slide movement isn’t applied to the velocity”. This inferred to me that you wanted it applied to the Rigidbody2D. I didn’t see you say you have a velocity variable but now I know that. :slight_smile:

So if the returned position is the same as the position prior to the slide then you’ve gone nowhere and can either completely reset the velocity to zero:

        var oldPosition = m_Rigidbody.position;
        var slideResults = m_Rigidbody.Slide(m_Velocity, Time.deltaTime, SlideMovement);
        if (slideResults.position == oldPosition)
            m_Velocity = Vector2.zero;

… or remove the velocity component in the direction of the slide hit normal:

        var oldPosition = m_Rigidbody.position;
        var slideResults = m_Rigidbody.Slide(m_Velocity, Time.deltaTime, SlideMovement);
        if (slideResults.position == oldPosition)
        {
            var hitNormal = slideResults.slideHit.normal;

            // NOTE: This might produce fractional values so if close to zero, you can always reset it to zero!
            m_Velocity -= Vector2.Dot(m_Velocity, hitNormal) * hitNormal;
        }

This depends on what you’ve selected for the SlideMovement options. If you’ve selected “UseNoMove” or “UseSimulationMove” then the Rigidbody2D.position isn’t changed yet so you can simply do:

        var slideResults = m_Rigidbody.Slide(m_Velocity, Time.deltaTime, SlideMovement);
        if (slideResults.position == m_Rigidbody.position)
            m_Velocity = Vector2.zero;

Obviously you could check this for the position X only instead if that’s better for you.

Hope that helps.

1 Like

Thanks that helps a lot, I’ve got it working how I want it to, I check if there was a slide collision and subtract the velocity component along the slide hit normal from the velocity if so. I didn’t really understand what information I have with SlideResults but I understand it a lot more now with your suggestions

Yeah my bad, should’ve been more clear i was talking about my own velocity variable

Thanks again for the help

1 Like