# Newbie here. Need help with updating direction in player.gd

**URL:** <https://forum.godotengine.org/t/newbie-here-need-help-with-updating-direction-in-player-gd/49230>\
**Category:** Help\
**Tags:** godot-4, gdscript\
**Created:** [February 24, 2024, 6:20am UTC](https://forum.godotengine.org/t/newbie-here-need-help-with-updating-direction-in-player-gd/49230 "2024-02-24T06:20:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DaveESso](https://forum.godotengine.org/letter_avatar_proxy/v4/letter/d/85f322/32.png) [@DaveESso](https://forum.godotengine.org/u/DaveESso)\
**Post date:** [February 24, 2024, 6:20am UTC](https://forum.godotengine.org/t/newbie-here-need-help-with-updating-direction-in-player-gd/49230/1 "2024-02-24T06:20:12Z")

</div>

### Godot Version

4.2

### Question

After struggling to debug for two hours, I eventually gave up. Below is the code from my player.gd file. My intention is to trigger the side\_walk animation when moving up or down, depending on the lastDirection variable, which stores either “left” or “right”. However, I encountered an issue where pressing the up or down keys always results in the lastDirection variable printing “left”, even if I had just pressed the right button before moving up or down. Consequently, the side\_walk animation consistently flips. How can I resolve this problem?

```auto
extends CharacterBody2D

@export var speed: int = 200
@onready var animation = $AnimatedSprite2D 
var lastDirection = ""

func handleInput():
	var moveDirection = Input.get_vector("ui_left","ui_right", "ui_up", "ui_down")
	velocity = moveDirection * speed
	
	if moveDirection.x != 0: # If moving horizontally
		if moveDirection.x < 0:
			lastDirection = "Left"
		else:
			lastDirection = "Right"

func updateAnimation():
	
	if velocity.length() == 0:
		animation.play("side_idle")
	else:
		if lastDirection == "Left":
			animation.flip_h = true
			animation.play("side_walk")
			print(lastDirection)
		elif lastDirection == "Right":
			animation.flip_h = false
			animation.play("side_walk")
			print(lastDirection)
			
		if velocity.x != 0 or velocity.y != 0:
			print(lastDirection)
			animation.play("side_walk")

func _physics_process(delta):
	handleInput()
	move_and_slide()
	updateAnimation()

```

---

<div class="post-metadata">

**Author:** ![dbat](https://forum.godotengine.org/user_avatar/forum.godotengine.org/dbat/32/7154_2.png) [@dbat](https://forum.godotengine.org/u/dbat)\
**Post date:** [February 24, 2024, 7:46am UTC](https://forum.godotengine.org/t/newbie-here-need-help-with-updating-direction-in-player-gd/49230/2 "2024-02-24T07:46:23Z")

</div>

First, please format your code with the \</\> tool. It’s so hard to read it.

I think, just a head-run here, that your moveDirection.x may often be \< 0. Therefore it’s constantly setting lastDirection to “Left”

---

<div class="post-metadata">

**Author:** ![pompyproductions](https://forum.godotengine.org/letter_avatar_proxy/v4/letter/p/9e8a1a/32.png) [@pompyproductions](https://forum.godotengine.org/u/pompyproductions)\
**Post date:** [February 26, 2024, 12:53am UTC](https://forum.godotengine.org/t/newbie-here-need-help-with-updating-direction-in-player-gd/49230/3 "2024-02-26T00:53:51Z")

</div>

Hello, it’s not necessarily the answer in and of itself, but I’m wondering if these could help you:

- It could be more straightforward to actually keep the last direction as a vector (`lastDirection = moveDirection`), and do the `if moveDirection.x < 0` check in the updateAnimation method.
- You’re repeating both `animation.play("side_walk")` and `print(lastDirection)` for every condition in your if/else statement in updateAnimation. You can place them right under “else” so that the rest of the code only handles the “sprite flipping”.

Sorry that this is not an actual answer, but these little things can sometimes help narrow down the places to look at 🙂
