This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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 physicsprocess(delta):
var axisX = Input.getactionstrength("uileft") - Input.getactionstrength("uiright")

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

func getinput():
if Input.is
actionpressed("uiright"):
velocity.x += 1
$AnimatedSprite.play("walk")
if Input.isactionpressed("ui_left"):
velocity.x -= 1
$AnimatedSprite.play("walk")
else:
velocity.x = 0
velocity = velocity.normalized() * speed
$AnimatedSprite.play("idle")

Godot version GLES3 Engine v3.5.1
in Engine by (12 points)

1 Answer

0 votes

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

by (286 points)

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.

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.

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.