Recently I started making my new 2d game with top-down movement and wanted to implement feature where when player moves - he's rotation changes through AnimationPlayer, but I've faced some problems. When I change the direction from left to right, from up to down, visa versa, animation always restarts. I can’t realy understanf what causes this if I have already a line that needs to erase this problem if animation_player.current_animation!=“walk”: .
Here is my code:
extends CharacterBody2D
var speed = 400.0 @onready var animation_player: AnimationPlayer = $AnimationPlayer
func _physics_process(delta: float) → void:
var direction := Input.get_vector(“left”,“right”,“up”,“down”)
if direction!=Vector2.ZERO:
velocity = direction * speed
if animation_player.current_animation!=“walk”:
animation_player.play(“walk”)
else:
velocity=velocity.move_toward(Vector2.ZERO,speed)
var rotation_speed:=2.0
var rotation_distance:float=abs(rotation)
var rotation_duration:=rotation_distance / rotation_speed
var tween:=create_tween()
tween.tween_property(self,“rotation”,0.0,rotation_duration)
if animation_player.current_animation!=“RESET”:
animation_player.play(“RESET”)
move_and_slide()
I can’t tell where the indentations in your code are. Does it look like this:
func _physics_process(delta: float):
var direction := Input.get_vector("left","right","up","down")
if direction != Vector2.ZERO:
velocity = direction * speed
if animation_player.current_animation != "walk":
animation_player.play("walk")
else:
velocity=velocity.move_toward(Vector2.ZERO,speed)
var rotation_speed:=2.0
var rotation_distance:float=abs(rotation)
var rotation_duration:=rotation_distance / rotation_speed
var tween:=create_tween()
tween.tween_property(self,"rotation",0.0,rotation_duration)
if animation_player.current_animation!= "RESET":
animation_player.play("RESET")
move_and_slide()
Edit: Although I’m guessing it looks like this:
func _physics_process(delta: float):
var direction := Input.get_vector("left","right","up","down")
if direction != Vector2.ZERO:
velocity = direction * speed
if animation_player.current_animation != "walk":
animation_player.play("walk")
else:
velocity=velocity.move_toward(Vector2.ZERO,speed)
var rotation_speed:=2.0
var rotation_distance:float=abs(rotation)
var rotation_duration:=rotation_distance / rotation_speed
var tween:=create_tween()
tween.tween_property(self,"rotation",0.0,rotation_duration)
if animation_player.current_animation!= "RESET":
animation_player.play("RESET")
move_and_slide()
Being ‘old school’, I do miss the ‘endif’ keyword, instead of just letting the indentation do the talking. For my part, I now add, as a comment, ‘### endif’ to keep track, visually, of where the ‘if’ statements finish. Just sayin’.
Doesn’t abrogate one’s responsibility to format their code when posting.
@Dad3353 TBH if you didn’t format it and had ### endif, I wouldn’t have noticed because I’d ignore your code until it was formatted. As would most people on here. Though we do tell newbies how to do it. This is just Python formatting. You get used to it and then it’s easy to parse without commenting if/loop/func endings.
Plus, this is valid code if there’s only one execution statement: