SparrowSparrow Posts: 15Member

Hi All

can anyone help me with smoother movement, i have pretty good movement from the doc's but it's not as responsive and doesn't give much control for smaller movement. does the velocity need to be * delta to smooth or is there something else i can do to improve it?

code below

extends KinematicBody2D

export (int) var speed = 200

var velocity = Vector2()

func get_input():
    velocity = Vector2()
    if Input.is_action_pressed('ui_right'):
        velocity.x += 1
    if Input.is_action_pressed('ui_left'):
        velocity.x -= 1
    if Input.is_action_pressed('ui_down'):
        velocity.y += 1
    if Input.is_action_pressed('ui_up'):
        velocity.y -= 1
    velocity = velocity.normalized() * speed

func _physics_process(delta):
    get_input()
    move_and_slide(velocity)

Answers

  • TwistedTwiglegTwistedTwigleg Posts: 1,049Admin
    Answer ✓

    The move_and_slide function already multiplies the velocity by delta, so you do not need to worry about doing that. delta will not necessarily make your movement smoother, but it will make your movement more consistent across differing frame rates, which you probably want so the game plays the same in almost all situations. That said, you shouldn't need to worry about it since delta is already handled in move_and_slide.

    I think what you are missing that would really help for smaller movements is acceleration and deceleration. I do not have an example handy, but here is a snippet for how to apply acceleration and deceleration (albeit in Unity, but it shouldn't be hard to translate to Godot. If you have issues, let me know and I'll see if I can whip something up)

    Another good place to look would be the Godot demo repository kinematic character demo. If I recall correctly, it does not have deceleration, but it might serve as a good reference to start building on.

    Other than that, I'm not really sure what else to suggest. I've found that making player scripts that feel right is a lot of fiddling around until it works for the game. Hopefully this helps :smile:

  • SparrowSparrow Posts: 15Member
    edited February 12

    @TwistedTwigleg said:
    The move_and_slide function already multiplies the velocity by delta, so you do not need to worry about doing that. delta will not necessarily make your movement smoother, but it will make your movement more consistent across differing frame rates, which you probably want so the game plays the same in almost all situations. That said, you shouldn't need to worry about it since delta is already handled in move_and_slide.

    I think what you are missing that would really help for smaller movements is acceleration and deceleration. I do not have an example handy, but here is a snippet for how to apply acceleration and deceleration (albeit in Unity, but it shouldn't be hard to translate to Godot. If you have issues, let me know and I'll see if I can whip something up)

    Another good place to look would be the Godot demo repository kinematic character demo. If I recall correctly, it does not have deceleration, but it might serve as a good reference to start building on.

    Other than that, I'm not really sure what else to suggest. I've found that making player scripts that feel right is a lot of fiddling around until it works for the game. Hopefully this helps :smile:

    Thanks Twisted

    yes changed the input to below, added an acceleration and hashed out the normalize and set a min and max to set full speed
    my area is quite small so values might be different on more open levels.

    var acceleration = 150
    var maxspeed = 500
    
    func get_input():
        velocity = Vector2()
        if Input.is_action_pressed('ui_right'):
            velocity.x = min(velocity.x + acceleration, maxspeed)
        if Input.is_action_pressed('ui_left'):
            velocity.x = max(velocity.x - acceleration, -maxspeed)
        if Input.is_action_pressed('ui_down'):
            velocity.y = min(velocity.y + acceleration, maxspeed)
        if Input.is_action_pressed('ui_up'):
            velocity.y = max(velocity.y - acceleration, -maxspeed)
        #velocity = velocity.normalized() * speed
    
  • SparrowSparrow Posts: 15Member
    edited February 12

    :)

  • TwistedTwiglegTwistedTwigleg Posts: 1,049Admin

    Nice! I'm glad it is working :smile:

Sign In or Register to comment.