0 votes
extends KinematicBody2D

const SPEED = 35
const GRAVITY = 10
const JUMP_POWER = -135
const FLOOR = Vector2(0, -1)

var velocity = Vector2()

var on_ground = false

func physicsprocess(delta):

if Input. is_action_pressed("ui_right"):
        velocity.x = SPEED
        $AnimatedSprite.play("Moving")
        $AnimatedSprite.flip_h = false
        if sign($Position2D.position.x) == -1:
            $Position2D.position.x *= -1
elif Input. is_action_pressed("ui_left"):
        velocity.x = -SPEED
        $AnimatedSprite.play("Moving")
        $AnimatedSprite.flip_h = true
        if sign($Position2D.position.x) == 1:
            $Position2D.position.x *= -1
else: 
    velocity.x = 0
    if on_ground == true:
        $AnimatedSprite.play("Idle")

if Input. is_action_pressed("ui_up"):
        if on_ground == true:
            velocity.y = JUMP_POWER
            on_ground = false

velocity.y += GRAVITY

if is_on_floor():
    on_ground = true
else:
        on_ground = false
        if velocity.y < 0:
            $AnimatedSprite.play("Jump")
        else:
            $AnimatedSprite.play("Falling")
        velocity = move_and_slide(velocity, FLOOR)
in Engine by (12 points)

the line: velocity = move_and_slide(velocity, FLOOR) It has identation, shouldn't it be outside the previous if-else?

1 Answer

0 votes

1) Add some console output see if everything is called and has the values as you expect.
2) You can comment out most of the method and just keep what you think should work; so for instance, just keep velocity y or x, see if it works, then un-comment small bits of code to find out what breaks your logic.

Using these two, you should easily be able to find your issue! Otherwise, can't really see what's wrong. Could be is_on_floor()returns false, but who knows..

by (125 points)
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.