First Person Slide in direction of rigidbody movement and not look direction


I am trying to implement a slide mechanic to my first-person game similar to Apex Legends.

Currently, my slide mechanic is very roughly implemented and doesn’t feel great. I am currently using a Rigidbody movement system with velocity. I want my slide mechanic to move the player in the direction that the velocity is moving the rigidbody and not the direction that the player is looking.

So if a player is moving North, but is looking behind them (South) and decides to slide, the slide should move the player North and not South where the player is currently looking.

This is my current code sliding. I know it’s not great :slight_smile:

            if (playerVelocity > 6)
                rb.AddForce(transform.forward * slideSpeed, ForceMode.VelocityChange);
                timePassed = 0f;

The issue seems to be coming from “transform.forward” however, I am still not sure how I can get get the direction of the rigidbody movement and keep moving in that direction.

Any help would be greatly appreciated!

Since there seems to be no answer here, I will put one here.

The solution is very simple. Just create a Vector3 variable to store the RigidBody velocity when the condition is met, and then use that variable instead of transform.forward. This should store the vector you are moving at before we apply any sliding.

So, a code snippet for this should look like this:

private Vector3 slideDirection;

if(playerVelocity > 6)
         slideDirection = rb.velocity;
         rb.AddForce(slideDirection * slideSpeed, ForceMode.VelocityChange);