Trouble making the player accelerate with alternating button presses

Godot Version

Godot v4.3

Question

I’m attempting to make a skating game where the player has to hold forward(w) while alternating between pressing left(a) and right(d) in order to gain speed. I’ve been able to make the inputs work close enough, but I’m at a complete loss as to how to increase the players speed with each button press. Any help with this matter would be greatly appreciated. I have included my most successful code thus far. And yes, I am aware that this code would not require the player to alternate button presses, as they could just hold left or right to go fast (if I knew how to make them go fast).

    public override void _PhysicsProcess(double delta)
    {
        if (Input.IsActionPressed("forward"))
        {
            if (Input.IsActionPressed("left")) 
            {
        
                GD.Print("left pressed!");
                //increase player speed by an amount

            }
            if (Input.IsActionPressed("right"))
            {
                GD.Print("right pressed!");
                //increase player speed by an amount

            }

        }
    }

Acceleration = Force/Mass

Velocity += acceleration

You can ignore mass, but you just need to decide on an appropriate force to create an acceleration.

If this is a rigid body you can call apply_central_force()

If this is a character body you can increase the velocity.

velocity += normalized_direction_vector × (force × delta)

Thank you kindly. This helped me make some progress.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.