All Questions

Community

D
DarkLordLeon
posted to: Creating the Enemy

What the difference between the handling of _velocity.x and y

Doc says:

linear_velocity is the velocity vector in pixels per second. Unlike in move_and_collide(), you should not multiply it by delta — the physics engine handles applying the velocity.

Why not just assigh _velocity.y = gravity in _ready() ? What's the difference?

  • Nathan Lovato replied

    Someone else asked a similar question. You can find the answer here: https://gdquest.mavenseed.com/community/188-trouble-wrapping-my-head-around-line-12

    Quoting the answer:

    The move_and_slide function takes an input velocity to move the character in a given direction, at a certain speed. That's what a velocity is.
    Internally, the function moves the character and handles collisions, making the character slide a bit against walls or the floor it hits, rather than getting stuck. To do so, it projects the remaining portion of velocity vector after the collision along the collision surface. That's where the "slide" comes from. That only happens when there's a collision, but in that case, the velocity changes inside the function.
    That is why it returns a new velocity. Hitting the floor or a wall should slow down your character. The returned velocity from the move_and_slide function does that: when your entity collides with something, the function will return a different velocity that takes that into account. The velocity in question depends on the angle of the collision and the surface you collided with.
  • D
    DarkLordLeon replied

    Thanks for reply. But I don't think there are same questions. I knew what move_and_slide dose, I'm confusing with why we need this line:

    _velocity.y += gravity * delta

    to handle y manually.

    Why don't assign a value in _ready() like:

    _velocity.y = speed.y 


    What the difference between them.


  • Nathan Lovato replied

    The gravity gives you a vertical acceleration. It's what makes you slow down and fall smoothly when you jump or walk off a ledge.

    If you assigned a fixed value to the Y velocity, the character would always move down at a constant speed.