My code seems fine but my character is stuck in place and I don't know how to fix it

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Trazmaball
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 _physics_process(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)

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

estebanmolca | 2019-12-28 09:30

:bust_in_silhouette: Reply From: nwgdusr999
  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…