How can I get my Flip H to work

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

In my code, i have a variable detecting which direction my character should be facing. The variable works just fine, but when I try to use it to set the flip h on my player sprites, it just doesn’t work. Here’s the code I have refering to the input axis variable and flipping the h of my sprites

var input_axis = 0

func update_animations():
#Detect what direction the player is moving and if the player is moving
if Input.is_action_pressed(“Move_Left”):
var input_axis = -1
if Input.is_action_pressed(“Move_Right”):
var input_axis = 1
if Input.is_action_pressed(“Move_Left”) and Input.is_action_pressed(“Move_Right”):
var input_axis = 0
if not Input.is_action_pressed(“Move_Left”) and not Input.is_action_pressed(“Move_Right”):
var input_axis = 0

if input_axis == -1:
	$Skull.flip_h = true
	$Scarf.flip_h = true
	$torso.flip_h = true
	$legs.flip_h = true
if input_axis == 1:
	$Skull.flip_h = false
	$Scarf.flip_h = false
	$torso.flip_h = false
	$legs.flip_h = false
:bust_in_silhouette: Reply From: zeludtnecniv

Your variable input_axis is define in your if and do not exist anymore when you test if it’s equal to -1 or 1.
Try this code instead of the 4 first condition :

var input_axis = 0
if Input.is_action_pressed("Move_Left"):
    input_axis -= 1
if Input.is_action_pressed("Move_Right"):
    input_axis += 1

It worked! Thanks for the help

Luke Fenstad | 2023-06-25 14:56