player spawning animation???

Godot Version

`4.4

Question

well i am a beginner in godot 4.4 and i want to add player spawning animation but but but my code is bit different this is my code

extends CharacterBody2D

const SPEED = 400.0
const JUMP_VELOCITY = -730

@onready var sprite_2d: AnimatedSprite2D = $Sprite2D

var jump_count = 0

func _physics_process(delta: float) -> void:
	# Add the gravity.
	if is_on_floor():
		jump_count = 0
		
		if (velocity.x > 1 || velocity.x < -1):
			sprite_2d.animation = "running"
		else:
			sprite_2d.animation = "default"
	else:
		velocity += get_gravity() * delta
		if (jump_count == 2):
			sprite_2d.animation = "double_jump"
		else:
			sprite_2d.animation = "jumping"


	# Handle jump.
	if Input.is_action_just_pressed("jump") and jump_count < 2:
		velocity.y = JUMP_VELOCITY
		jump_count += 1
	# 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, 30)

	move_and_slide()
	
	var isLeft = velocity.x < 0
	sprite_2d.flip_h = isLeft

What do you mean your code is different?
What’s the exact problem you’re having with adding the animation to your script? Did you try and get any error?

If you want to stick with your current logic, you have to add another bool (e.g. var spawning = true) to check if your character is spawning. Assuming that you want to play this animation when you add player to the scene tree, you can embed this logic in the _ready() function instead of the _physics_process. You’ll need to make sure that the default animation doesn’t immediately override the spawning animation. Use the spawning bool to prevent this.

It’s a bit cleaner and easier to expand on if you refactor your animation switches using centralized transition rules akin to a state machine. However, if these are the only animations you’ll ever have in your game, do not bother.

i dont know how to add animation

uhhh i dont understand

Could you rephrase your question then? I’m not sure where you’re getting stuck. Don’t you know how to add spriteframes to an AnimatedSprite2D? Don’t you know how to play it? Or are you getting stuck in the programming logic?

well i am just a beginner and i know basics of godot like how to add nodes or spritesheet but i am confused in gdscript i dont know what to type or what to use i tried to find toturial on youtube but nothing helps me can you tell me where and what do i need to type

I reckon you’re better served by taking a step back and learning the basics of programming before jumping back into the engine.

uhhh i think i should finish my project before learning language

Well, are you learning anything by having other people and stitched together tutorials finish your project?

well i work alone and i dont have anyone to help me

That’s not what I’m getting at. Programming is a huge part of game development. If you can’t control the flow of logic there’s really not that much that we can do to help you. Apart from writing whatever feature you try to implement for you that is. If I were to do that, it’s not helping your project. It’s just stitching another bit of code that you probably don’t fully understand to your project.

You can either learn to code yourself, or pay a software developer to do the work for you.

CS50 for a solid introduction to coding (in python). Or if you prefer to start in GDscript Learn GDscript from Zero.

Best of luck with your game dev journey.

1 Like

uh i think you are right thanks