How do I fix my character not moving forward?

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

I’m currently stuck. I’m trying to make this character move left and right with different animations for it. However i noticed that when I would try and press the right button it doesn’t move. like no animation and doesn’t move. It moves left with the animation.

code below_

extends KinematicBody2D

export (int) var speed = 45
onready var animatedSprite = $AnimatedSprite

var velocity = Vector2()

func _physics_process(delta):
var axisX = Input.get_action_strength(“ui_left”) - Input.get_action_strength(“ui_right”)

if axisX != 0:
	animatedSprite.animation = "walk"
else:
	animatedSprite.animation = "idle"
get_input()
velocity = move_and_slide(velocity)

func get_input():
if Input.is_action_pressed(“ui_right”):
velocity.x += 1
$AnimatedSprite.play(“walk”)
if Input.is_action_pressed(“ui_left”):
velocity.x -= 1
$AnimatedSprite.play(“walk”)
else:
velocity.x = 0
velocity = velocity.normalized() * speed
$AnimatedSprite.play(“idle”)

:bust_in_silhouette: Reply From: Cire_arievilo1

“uiright” it is written wrong the correct one would be “ui_right”
the same thing with “uileft” is misspelled in some parts of the code.

I checked my code and in it i’s actually spelled “ui_right” and I’m now unsure what’s going on, I’ve also noticed that when I add the left movement the right stops working now.

PandaGuy68 | 2022-12-28 20:06

test this code:

extends KinematicBody2D

export (int) var run_speed = 100
export (int) var jump_speed = -400
export (int) var gravity = 1200
onready var animatedSprite = $AnimatedSprite
var velocity = Vector2()
var jumping = false

func get_input():
    velocity.x = 0
    var right = Input.is_action_pressed('ui_right')
    var left = Input.is_action_pressed('ui_left')
    var jump = Input.is_action_just_pressed('ui_select')

if jump and is_on_floor():
    jumping = true
    velocity.y = jump_speed
if right:
    animatedSprite.play("walk")
    velocity.x += run_speed
if left:
    animatedSprite.play("walk")
    velocity.x -= run_speed

if velocity == 0:
    animatedSprite.play("idle")

func _physics_process(delta):
    get_input()
    velocity.y += gravity * delta
    if jumping and is_on_floor():
        jumping = false
    velocity = move_and_slide(velocity, Vector2(0, -1))

untested code, may contain errors.

Cire_arievilo1 | 2022-12-29 03:03