New to coding, how do you make an animation play?

Godot version 4

I’m new to this kind of coding. I’ve coded with blocks and that’s all, though because of this I’m able to get smooth movement to work. However, I have no clue how to play animations. I’m making a farming game and am using the animated sprite 2d because I don’t know how animation players work.

extends StaticBody2D

func get_input():
var input = Vector2()
if Input.is_action_pressed(“Clicked”):
$AnimatedSprite2D.play(“growing.demo”)

This doesn’t work and I don’t know why. I added a hit box and it still wont work. Any clue how to fix this? appreciate it :D!

1 Like

Ok ill help you

Ok i think i know the cause. You need to go to the project settings and set an input for left (set it to A) , right (set it to D) and jump (set it to space). Then create a new scene called Player and add a CharacterBody2D, then as a child, add a collision shape 2D and an animated sprite 2D and put the animation in here. Next create an empty sprite in the CharacterBody2D and put in this script:

extends CharacterBody2D


@export var SPEED = 300.0
@export var JUMP_VELOCITY = -400.0


func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction := Input.get_axis("left", "right")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	
	var left := ("left")
	
	if Input.is_action_just_pressed("left"):
		$AnimatedSprite2D.play("growing.demo")

	move_and_slide()

Then when you press A, it should play the animation. I hope this helped! :slight_smile:

Wait! You also need to add a animation tree to the CharacterBody2D and create the animation there

Ok, so you don’t need an animation-tree. Your code looks good, but I see two possible problems here:

  1. Are you calling get_input somewhere? Maybe you mean to override the built-in _input?
  2. As @pro_core3 suggests, did you specify “Clicked” in the input map?

Since your are new to coding, I suggest you look into how to debug your code.
A simple first step would be to add some print statements to see if the code is actually executed.
Even better would be to look into the debugging tools to analyze the execution of your code (Overview of debugging tools — Godot Engine (stable) documentation in English). Debugging is invaluable and will save you some trips to the forum :wink:

Also the AnimationPlayer is quite easy to grasp. You can find a great intro here: Animation — Godot Engine (stable) documentation in English.
In general I would suggest to look at the official documentation. There are great resources there to get you started. :slight_smile:

4 Likes

Thank you :D! I did specify “clicked” to be left mouse button :3.
Thanks for the links, appreciate it!

Ok, what about 1., are you calling get_input somewhere?

Btw. input-actions are case-sensitive, so “Clicked” is not the same as “clicked”.

for 1 I wasn’t sure how to answer, I just assumed I was supposed to use get_input because I was clicking it (?) not sure how much of this makes since, again, I’m new TToTT (i miiighhhtt have to go change my input actions…)

get_input is not a built-in function. That means, that you are creating a new function with the func keyword, but it is never called.

Here is a tutorial that explains how to handle input: Input examples — Godot Engine (stable) documentation in English

But I suggest you take a step back and look at the getting started section: Introduction — Godot Engine (stable) documentation in English. There is even an intro to coding in there (I haven’t looked at it myself, so I can’t vouch for it).

1 Like

ooohhhh, thank you :D!!! this actually helps a lot oml (I have put get_input on like, 7 different codes (they don’t work) so Imma go fix that :3, thanks again :D!)