Godot Version
Godot Engine v4.2.2.stable.official
Question
I’m making a mobile game and want to use mobile joystick for movement. I’ve a logic for changing character animations depending on x,y values. But when I move my character animations display a bit wrong. Like: when he walks “more left than down” - it still display “walk down animation”. Similar situation with the right upper corner. It just go right. I’m trying to figure out where I have made a mistake but it’s kinda hard. Can anyone help please?
p.s. Is there is a way to make all those “if” & “elif” more simple?
extends CharacterBody2D
@export var SPEED = 3000
#@onready var animated_sprite = $AnimatedSprite2D
@onready var animations = $AnimationPlayer
@onready var joystick = $"../Joystick"
func handleInput(x):
# movement direction
#var moveDirection = Input.get_vector( "moveLeft","moveRight", "moveUp", "moveDown")
var moveDirection = joystick.posVector
# applies movement
velocity = moveDirection * SPEED * x
func updateAnimation():
if velocity.length() ==0:
if animations.is_playing():
animations.stop()
else:
#Second way
var direction = "Down"
if velocity.x < 0 && velocity.y < 0 && velocity.y > velocity.x || velocity.x < 0 && velocity.y == 0: direction = "Left"
elif velocity.x < 0 && velocity.y > 0 && velocity.y - velocity.x < 0 || velocity.x < 0 && velocity.y == 0: direction = "Left"
elif velocity.x > 0 && velocity.y > 0 && velocity.y < velocity.x || velocity.x > 0 && velocity.y == 0: direction = "Right"
elif velocity.x > 0 && velocity.y < 0 && velocity.x - velocity.y > 0 || velocity.x > 0 && velocity.y == 0: direction = "Right"
elif velocity.x > 0 && velocity.y < 0 && velocity.x - velocity.y < 0 || velocity.y < 0 && velocity.x == 0: direction = "Up"
elif velocity.x < 0 && velocity.y < 0 && velocity.x > velocity.y || velocity.y < 0 && velocity.x == 0: direction = "Up"
elif velocity.x < 0 && velocity.y > 0 && velocity.y - velocity.x > 0 || velocity.y > 0 && velocity.x == 0: direction = "Down"
elif velocity.x > 0 && velocity.y > 0 && velocity.x < velocity.y || velocity.y > 0 && velocity.x == 0: direction = "Down"
#Old working way
#var direction = "Down"
#if velocity.x < 0: direction = "Left"
#elif velocity.x > 0: direction = "Right"
#elif velocity.y < 0: direction = "Up"
animations.play("walk" + direction)
func _physics_process(delta):
handleInput(delta)
move_and_slide()
updateAnimation()