Why is my animation spazzing out?

Godot Version

v4.1.2.stable.official [399c9dc39]

Question

When I execute this code, I attempt to play an animation. Why does the ‘zombieidle’ animation not jitter or appear to repeat rapidly like it’s freaking out, while the ‘zombiewalk’ animation does? I feel like I’ve tried everything, but im not too good at gdscript or godot yet so chances are one of you can help.

I’m a new user so I can’t upload an attachment unfortunately. If anyone could help, it would be much appreciated, I am new to all of this :smile:

extends CharacterBody3D


var speed = 1
var move = 0
var rotspeed = 0.01

var is_moving = false

func _ready():
	randomize()


func _physics_process(delta):
	var forward = -global_transform.basis.z.normalized()
	var anim = $zombieidle/AnimationPlayer
	
	if is_moving == true:
		anim.play("zombiewalk")
	
	if is_moving == false:
		anim.play("zombieidle")
	
	
	if velocity.length() > 0.9:
		velocity = velocity.normalized() * 0.8
	
	#if velocity.length() == 0:
		#anim.play("zombieidle")
	#else:
		#anim.play("zombiewalk")
	
	if move == 1:
		is_moving = true
		velocity -= forward * speed
	if velocity.length() > 1.6:
		velocity -= velocity
		is_moving = false
	
	if move == 2:
		rotate_y(-rotspeed)
	
	
	move_and_slide()
	

func _on_timer_timeout():
	move = randi_range(1,6)
	print(move)

Without a way to visualize it, it is very difficult to give you a proper answer, but what I can remember from 2D animations is that when you do this:

if is_moving == true:
		anim.play("zombiewalk")

the animation goes back to the beginning.
I AM ASSUMING HERE.

I would add something like

if is_moving == true:
		if not anim.is_playing():
				anim.play("zombiewalk")

Report back!

Hey, I ended up solving it. To be completely honest, I’m not entirely sure what I did to fix it. But it’s resolved now. I’m going to get some sleep and work on this more tomorrow. Thank you for the help, though.

extends CharacterBody3D

# zombie speed
var speed = 1

# ai (do not edit)
var move = 0

# zombie rotation speed
var rotspeed = 0.01

# is_moving handles the animations
var is_moving = false

func _ready():
# randomizes the seed for rng
	randomize()


func _physics_process(delta):
	
	# this makes the zombie move in the direction they are facing
	var forward = -global_transform.basis.z.normalized()
	
	# reference animation player
	var anim = $zombieidle/AnimationPlayer
	
	# checks if zombie is moving, if it is, play walk anim
	if is_moving == true:
		anim.play("zombiewalk")
	
	# checks if zombie is not moving, if it isn't, play idle anim
	if is_moving == false:
		anim.play("zombieidle")
	
	# checks if velocity equals zero, if it does, sets is_moving to false
	if velocity.length() == 0:
		is_moving = false
	
	# this limits the max velocity of the zombie, prolly a better way to do this though
	if velocity.length() > 0.9:
		velocity = velocity.normalized() * 0.8

# moves the zombie forward, also sets is_moving to true, so the walking animation plays
	if move == 1:
		is_moving = true
		print(is_moving)
		velocity -= forward * speed
	if velocity.length() > 1.6:
		velocity -= velocity

# rotates the zombie
	if move == 2:
		rotate_y(-rotspeed)
		print(is_moving)
	move_and_slide()
	

# this makes generates a number between 1,6 deciding what the zombie will do
func _on_timer_timeout():
	move = randi_range(1,6)
	print(move)

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.