0 votes

Hi, I'm very new to Godot (2 days old) and I wanted to make a simple 2D platformer. I followed some tutorial on youtube and ended up having this code as my movement controller.


Here's a snippet of where my problem lies:

        func _physics_process(delta):
var x_input = Input.get_action_strength("right") - Input.get_action_strength("left")
if x_input != 0:
    motion.x += x_input * acceleration * delta
    motion.x = clamp(motion.x, -maxspeed, maxspeed)

    sprite.flip_h = x_input < 0
    if is_on_floor() and motion.x !=0:
    if not is_on_floor() or is_on_wall() or is_on_ceiling():
        motion.x = lerp(motion.x, 0, airfriction)

    if is_on_floor():
        motion.x = lerp(motion.x, 0, friction)
    if not is_on_floor() or is_on_ceiling():
        motion.x = lerp(motion.x, 0, airfriction)

motion.y += gravity * delta

if is_on_floor():
    jumpsleft = 3
    availjumps = 24
    if Input.is_action_just_pressed("jump"):
        isjumping = 1
        motion.y = -jumpforce
        jumpsleft = jumpsleft - 1

    if Input.is_action_just_pressed("Dodge") and availdodge > 0:
        motion.x = x_input * 2000
        availdodge = 0
        availdodge = 1

As you can see I clamped the motion.x to a maxspeed to stop the player from accelerating too much. But this also blocks my dashing (i wrote it dodge in the script). Basically what happens is that when i press dodge/dash i only teleports a few blocks forward and not dash like the way i imagined it would be. Which is more or less like dashing in any other platformer games. I tried raising the clamp whenever i press dodge but it results in me being unable to revert to the old maxspeed, and also i think it's not a really good solution to this?

Edit: If i remove the clamp i can dash just like i want but i keep on accelerating ):

That's about it. Thanks for reading, i hope i wrote it clear enough.

Godot version Godot_v3.3.2-stable_win64
in Engine by (12 points)

Please log in or register to answer this question.

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 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 webmaster@godotengine.org with your username.