Animation constantly changes

Godot Version

4.6

Question

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()

Can you show what the AnimationPlayer’s animations are?

Also tip:

When posting code on the forum, highlight it all and then press the ‘Preformatted text‘ button that looks like this: </>

That way the code’s easier to read.

The reset animation is just ordinary state of the Player’s rotation.

1 Like

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()

Is that correct?

Please format your code by pressing Ctrl+E in the editor and pasting your code in, or placing your code between three backticks like so:

```gd
#code here
```

Don’t make people guess about your code indentation. We literally had someone post today with bad indentation that was the source of all their errors.

1 Like

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:

if true: print("True")
1 Like