0 votes

Hi there. I'm currently working on a top/down game and wanted to make movement slippery whenever you are on ice or the like. This is how movement is controlled right now.
What would need to be changed or added into this so I can make slippery movement?

var motion = Vector2()  

motion.x = int(Input.is_action_pressed("d")) - int(Input.is_action_pressed("a"))
motion.y = int(Input.is_action_pressed("s")) - int(Input.is_action_pressed("w"))
motion = motion.normalized()
motion = move_and_slide(motion * ms)
Godot version 3.2
in Engine by (14 points)

1 Answer

0 votes

Below I am attaching a code that gives the player a slippery movement. The desired effect here is due to the linear_interpolate() method. You can further search the Godot Docs to know about this method for better clarification but what it does is, it makes sure that the motion won't become 0 suddenly. The value of motion will decrease by 0.05 and this stops the player slowly once you stop pressing the input key.

var MAX_SPEED = 500
var motion = Vector2()  

func _physics_process(delta):
    var input_vector = Vector2.ZERO
    input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
    input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")

    if input_vector != Vector2.ZERO:
        motion += input_vector * ACCELERATION
        motion = motion.clamped(MAX_SPEED)
        motion = motion.linear_interpolate(Vector2.ZERO, 0.05)

    motion = move_and_slide(motion)
by (414 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.