+1 vote

I'm right at the beginning of putting together a simple platformer with a KinematicBody2D main character. I've only gotten as far as adding basic movement controls, only to run up against a very strange bug - the character's jump height is inconsistent. First they jump too high, then too low, then too high again, alternating between high/low jumps.

This behavior grows more severe the higher I set them to jump; if set high enough, they rocket off the screen first time you jump, then barely jump at all, then rocket off screen again, then barely jump at all again, and so on. I've tried disabling the animations and switching between moveandslide() and moveandcollide() without effect. I also tried multiplying their movements by the delta, which didn't fix the bug but it's good practice so I left it in.

The character themselves is a simple KinematicBody2D with a sprite, an AnimationPlayer, a CollisionObject2D (a CapsuleShape2D around the character), and a Raycast2D straight down to detect the ground.

Here's the code:

extends KinematicBody2D

var agility = 1
var velocity = Vector2(0,0)

func _physics_process(delta):

    if $groundDetector.is_colliding():
        velocity.x = lerp(velocity.x, 0, .1)

        if Input.is_action_just_pressed("ui_up"):
            $AnimationPlayer.play("jump")
            var jump_power = 300 
            velocity += Vector2(0,agility * -jump_power)
        if Input.is_action_pressed("ui_left") and $groundDetector.is_colliding():
            velocity +=  Vector2(agility * -600 * delta,0)
            $AnimationPlayer.play("move")
        if Input.is_action_pressed("ui_right") and $groundDetector.is_colliding():
            velocity +=  Vector2(agility * 600 * delta,0)
            $AnimationPlayer.play("move")

    else:
        velocity += Vector2(0, 100* delta)

    move_and_slide(velocity)
Godot version 3.1.2
in Engine by (13 points)

Try:

if $groundDetector.is_colliding():
    velocity.x = lerp(velocity.x, 0, .1)
    velocity.y = 0

Ah, thank you! Of course, it was retaining some of the "falling" velocity after hitting the ground, invisible because it's a kinematic body. You're right, that solved it.

No problem! It's a lot easier to find mistakes in other people's code rather than your own haha

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 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.