Godot Version 4.6
Question
So none of my walk animations are playing
func _physics_process(_delta: float) -> void:
if Input.is_action_pressed("left") or Input.is_action_pressed("right") or Input.is_action_pressed("up") or Input.is_action_pressed("down"):
walk()
func walk():
current_state = States.WALK
character_direction.x = Input.get_axis("left", "right")
character_direction.y = Input.get_axis("up", "down")
if character_direction:
velocity = character_direction * movement_speed
if character_direction.x < 0:
%sprite.flip_h = true
%sprite.animation = "walk_LR"
elif character_direction.y < 0:
%sprite.flip_h = true
%sprite.animation = "walk_UD"
elif character_direction.x > 0:
%sprite.flip_h = false
%sprite.animation = "walk_LR"
elif character_direction.y > 0:
%sprite.flip_h = false
%sprite.animation = "walk_UD"
I know that the way I have the walk code is definitely not the most efficent, but it works, besides this issueâŚ
Also, is there any way I can easily condense my code for animation/flipping the sprite? The way I have it now feels really repetitiveâŚ
Edit: I know that the issue probably has to do with the walk function being called in the physics_process function, but I donât know what I should change to fix thatâŚ
Are your animations baked into a spritesheet?
Have you at least managed to get them playing separately?
Outside of the game playing, just in the player scene, the animations play.
Have you tried debug with print()?
Specially inside your walk function .
The first frame of animation always shows up when given the input, but the rest of the animation doesnât play, so I know the code is at least running. Sorry, I shouldâve specified a bit more in my original postâŚ
Well, using print, it printed out the phrase as long as I held the button. Like I said in the edit, it just confirmed that the problem has to do with the walk function being called within the physics process function. I just donât know where else Iâd call it.
Usually itâs issue of condition not be triggered or function not called .
Try use print() to figure out what and why is not working.
The walk function is being called and so it the animation. The animation is just constantly restarting because it keeps being reset because the walk function is called by the physics process function. I just donât know where else I should call it to fix this.
func _process()
Maybe ?
Just out of curiosity why donât You use
func _input() it be cleaner
func get_input():
direction_x = Input.get_axis("left", "right")
Since you have this walk state, I assume you also have an Idle state right? You should only play the animation at the start of the walk state.
This code right here will start the walk animation over and over.
You could do something like this to fix it.
func physics_process(_delta) -> void:
match current_state:
States.IDLE:
If Input.is_action_pressed("left") or Input.is_action_pressed("right") or Input.is_action_pressed("up") or Input.is_action_pressed("down"):
walk()
current_state = States.WALK
States.WALK:
##Your walk code
pass
This way, the animation in the walk() function only plays once when transitioning to the walk state.
With that said, you should also read about using _input(event) instead of polling Input every frame in the physics process.
Changing the animation property wonât automatically start playing the new animation if the AnimatedSprite is paused. Either use the play() method instead of changing the animation property, or unpause it once at the beginning (e. g. by selecting âAutoplay on Loadâ for one of the animations).
This still isnât workingâŚ
func _physics_process(_delta: float) -> void:
match current_state:
States.IDLE:
%sprite.animation = "idle"
if Input.is_action_just_pressed("up") or Input.is_action_just_pressed("down") or Input.is_action_just_pressed("left") or Input.is_action_just_pressed("right"):
current_state = States.WALK
States.WALK:
walk()
I might have just not done it right though?
Might help to share your scene setup.
Change this (and the similar lines) to %sprite.play(âwalk_LRâ) (replacing the â_LRâ with â_UDâ, etc.). Check to make sure you arenât playing the animation youâre about to play already before playing it:
%sprite.flip_h = ?
if %sprite.animation != âwalk_?â:
%sprite.play(âwalk_?â)
Hope this helps!
Iâm probably gonna try and restart the walk script and see what I did wrong because with the weird way I structured it, these solutions arenât working⌠If it happens again with newly structured code, then Iâll probably come back and try these solutions againâŚ
Well, this screenshot is not very helpful, since the nodes inside the player are hidden and I believe the animation issues are in that scene. Next time try to give as much useful information to let people understand the problem, so then they can try to solve it.
Anyways into your problem: Iâm assuming youâre using an AnimatedSprite that constantly is executing an animation in loop, and in side your WALK state youâre changing the animation. With this implementation, you donât need to call play/stop.
In my opinion, itâs a not-so-elegant code, but it can work, and thatâs what matters at the end of the day.
What I think itâs the issue (most probably) is that in every physics_process iteration the animation is getting reassigned, so the visuals are restarting continously.
As an example: you press LEFT, then the animation is set to âwalk_LRâ. You expect the AnimatedSprite to advance to frame 2, but the physics_process calls again and executes the line â%sprite.animation = âwalk_LRââ once more, reseting the animation to frame 1 another time.
A way to fix this issue (if itâs because of this and not another thing) would be to change/assign the animation only if the current one is different from the one you want to put.
Something like:
func walk():
current_state = States.WALK
character_direction.x = Input.get_axis("left", "right")
character_direction.y = Input.get_axis("up", "down")
if character_direction:
velocity = character_direction * movement_speed
if character_direction.x < 0:
%sprite.flip_h = true
if %sprite.animation != "walk_LR"
%sprite.animation = "walk_LR"
elif character_direction.y < 0:
%sprite.flip_h = true
if %sprite.animation != "walk_UD"
%sprite.animation = "walk_UD"
elif character_direction.x > 0:
%sprite.flip_h = false
if %sprite.animation != "walk_LR"
%sprite.animation = "walk_LR"
elif character_direction.y > 0:
%sprite.flip_h = false
if %sprite.animation != "walk_UD"
%sprite.animation = "walk_UD"
With this little patch and no re-coding, this might work.
Also, as a recommendation, I would recommend you assigning data to variables instead of calling them directly with %. I donât really know if thereâs a real performance issue in calling %Nodes so many times, but I would guess that it might be a little bit better and way cleaner to maintain and understand doing âvar sprite: Sprite = %spriteâ.