Character doesn't run right when ui_right pressed cannot see why

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By LifeRips

Here is the code: please help!

extends KinematicBody2D

const SPEED = 100
const GRAVITY = 10
const JUMP_POWER = -250
const FLOOR = Vector2(0, -1)
var isAttacking = false

var velocity = Vector2()

var on_ground = false


func _physics_process(delta):
	if Input.is_action_pressed("ui_right") && isAttacking == false:
		velocity.x = SPEED
		$AnimatedSprite.play("Run")
		$AnimatedSprite.flip_h = false
	if Input.is_action_pressed("ui_left") && isAttacking == false:
			velocity.x = -SPEED
			$AnimatedSprite.play("Run")
			$AnimatedSprite.flip_h = true
	else:
		velocity.x = 0
		if on_ground == true:
			if isAttacking ==false:
				$AnimatedSprite.play("Idle")
		
	if Input.is_action_pressed("ui_up"):
		if on_ground == true:
			velocity.y = JUMP_POWER
			on_ground = false
	if Input.is_action_just_pressed("Attack"):
		if on_ground == true:
			$AnimatedSprite.play("Attack")
			isAttacking = true

	velocity.y += GRAVITY
	
	if is_on_floor():
		on_ground = true
	else:
		on_ground = false
		if velocity.y < 0:
			$AnimatedSprite.play("Jump")
		else:
			$AnimatedSprite.play("Fall")
	
	velocity = move_and_slide(velocity, FLOOR)



func _on_AnimatedSprite_animation_finished():
	if $AnimatedSprite.animation == "Attack":
		isAttacking = false

Because gdscript is indent-sensitive, it’s really difficult to point out potential script problems in unformatted code. Please edit your post and format the code. To do that, just select all of the code and press the { } button in the editor’s toolbar.

jgodfrey | 2020-05-13 13:08

Hi, thanks for that.

LifeRips | 2020-05-13 17:01

:bust_in_silhouette: Reply From: kris_bsx

Is your variable “on_ground” true when you press “ui_right”?

If “on_ground” is true, I don’t see anything wrong, maybe your node is already colliding with another one, and that keeps him from moving?

If you have nodes in your scene that can collide with your character, try disabling them. Just keep the ground and test.

Hi thanks for the reply. Still no luck. Pressing right turns the character but freezes in space. What is confusing is that when the if statement for ui_left is taken out, then ui_right works fine.

LifeRips | 2020-05-13 17:03

Solved! I changed the second “if” to “elif”.

Thanks for the help!

LifeRips | 2020-05-13 17:07

Yes… I did not see the if … if … else!

Good if you solved your problem!!!

kris_bsx | 2020-05-13 19:55