Godot Version
4.7
Question
I am going through a tutorial on YouTube so I don’t have to ask so many questions here. On the Game Dev Tavern soccer tutorial, this code works. However, when I try to put it into my script, I get an error code saying “cannot call method ‘play’ on null value”.
I interpret that to mean that since the velocity value is 0, the “play” function is not permitted. The code should show the run animation when the player is moving more than 0 pixels per second and playing the idle animation when the player isn’t moving.
What’s going wrong here?
If you share code, please wrap it inside three backticks or replace the code in the next block:
class_name Player
extends CharacterBody2D
@export var speed : float
@onready var animation_player : AnimationPlayer = %AnimationPlayer
func _process(delta:float)->void:
var direction: = Input.get_vector("left", "right", "up", "down")
velocity = direction * speed
if velocity.length()>0:
animation_player.play("run")
else:
animation_player.play("idle")