All Questions

Community

s
shahnj

What makes the player stop on the horizontal plane?

Hi,

I was just wondering, the player moves horizontally while pressing the "key" down, however, when the player releases the "key" the character comes to a stop, what causes that? Is it the move_and_slide() function?

Thanks in advance!

  • s
    shahnj replied

    I thought about it again: It's not the move_and_slide() function, rather it's the when the player releases the "key", the action strength from the method get_action_strength() is reset to zero (direction.x = 0.0), which when multiplied with speed.x the resulting

    _velocity.x = speed.x * direction.x = 0.0

    Is that the correct reasoning? Although the move_and_slide() function also does reset the _velocity when colliding with an object.

    I was trying to see on how to introduce friction, that's why I came across the above question.

  • GDQuestions replied

    It's correct, the direction multiplies the speed by 0.

    To introduce friction, instead of instantly setting the velocity, you need to gradually increase or decrease it. People mostly do that using a function called lerp() (it means linear interpolation). It calculates a weighted average between two values.

    It works, but I'll recommend steering behaviors. It looks something like this in your case (note: I'm writing the code here and not testing it, but it's the general idea).

    var desired_x_velocity = speed.x * direction.x
    var steering = _velocity.x - desired_x_velocity
    _velocity.x += steering * delta * 8.0

    Let me explain how it works and then why it works well in many situations.

    You want to treat the currently instant velocity as some target velocity you want to reach over time (desired_x_velocity).

    To know how much the velocity has to change, you calculate the difference with the steering variable: it's how much my _velocity.x has to change to become desired_x_velocity.

    To get your friction, you add only a portion of that steering to the velocity every frame (that's the 3rd line).

    You'll notice I use a value of 8.0 there. It's completely arbitrary. It's just that delta is very small, so multiplying the steering just by delta would make the character accelerate too slowly.

    You want to adjust that value depending on how the character feels: it controls how much it accelerates or decelerates.

    Why use that steering equation?

    There are four reasons I often recommend steering equations:

    1. They make it easy to control the movement feel. Just change that "8.0" above.
    2. They work with 2D and 3D vectors (top-down and 3D movements) with the same code, only a couple of lines (other approaches require more code as you add dimensions of movement).
    3. They work well for both playable characters and AIs.
    4. It's little code.

    Here's an example of this steering equation applied to movement like in The Binding of Isaac (top-down view, 8 movement directions):

    var desired_velocity = speed * direction
    var steering = _velocity - desired_velocity
    _velocity += steering * delta * 8.0

    It's almost the same!

    I hope this help. 🙂

  • L
    Lotus replied

    It was a little tricky to figure out how to implement it. at first, the directions of the character got reversed, I did some changes and this is what I came up with: it makes the character slide a little when the button is released, feels like the character has momentum now.

    [code}
    extends Actor


    export var slide = 2.0


    func _physics_process(delta: float) -> void:

    var direction: = Vector2(

    Input.get_action_strength("move_right") - Input.get_action_strength("move_left"),

    1.0

    )

    var desired_velocity = speed.x * direction.x

    var steering = desired_velocity - velocity.x

    velocity.x += steering * delta * slide

    velocity = move_and_slide(velocity)
    [/code]