Skateboard Movement / Setting X Velocity Axis to 0

Background: This is a skateboarding game. The current setup uses Rigidbody.AddForce for acceleration, Transform.Rotate for turning, and sets Rigidbody.velocity directly in certain other places. Let’s assume the skateboard can only move in a forward direction.

I want to be able to decide how much turning reduces forward momentum, ranging from keeping only your initial movement vector projected in the direction of the board to keeping all of your forward momentum.

The first case seems like it should be simple, but I’m having difficulty. In this case, moving forward and then turning 90° should result in the skateboard coming to a complete stop (there may also be a conceptual problem here, but I’m less concerned about that).

Here’s the code thus far (simplified for clarity), with two different versions I’ve tried:

void FixedUpdate () {
    Rigidbody rb = GetComponent<Rigidbody>();

    transform.Rotate(Vector3.up, horizontalAxis * groundedTurnSpeed * Time.deltaTime);
    rb.AddRelativeForce(Vector3.forward * verticalAxis * accelerationSpeed);

    // Version 1
    // Change the velocity from global to local coordinates
    newVelocity = transform.InverseTransformVector(rb.velocity);
    // Get rid of the velocity that isn't along the forward axis of the skateboard
    newVelocity.x = 0;
    // Convert back to global coordinates
    rb.velocity = transform.TransformVector(newVelocity);

    // Version 2
    rb.velocity = Vector3.ProjectOnPlane(rb.velocity, transform.right);
}

Both of these versions successfully remove all of the movement in the player’s x direction, as expected, but the player does not slow down as quickly as expected. Instead of coming to a stop after turning 90 degrees (since that’s perpendicular to the initial direction of movement), the player can make several full turns and still have some forward momentum remaining.

My only guesses here are that FixedUpdate is such a small timestep that it’s not actually removing a measurable momentum every frame, or that some other underlying system is tripping me up.

When you turn, does your character snap 90 degrees in a single frame? If not, I would expect this behavior. Each FixedUpdate, the angle now considered “forward” changes. In real life, a skateboarder can also apply acceleration, then turn several times without applying more before coming to a stop.


If you specifically want the behavior where a gradual turn of 90 degrees fully stops you, you should instead hold onto a forward velocity stored as a vector each time the player applies acceleration, and every FixedUpdate, manually set the rigidbody to that, projected onto the current forward.