my version is godot 4.2.2
ı wanted to know the code for how to trigger an animation player when the player steps into the area2D
my version is godot 4.2.2
ı wanted to know the code for how to trigger an animation player when the player steps into the area2D
If the animation is part of that scene, you can use the $AnimatedSprite2D.play or similar. If you look at any of the Godot game tutorials on YouTube you can re-use the character animation code.
Is that what you’re asking? Or something else?
its just that ı have tried many times and they generally failed, ıll give you the code ı used so you can maybe help me with what ım missing
it was useally like this:
extends Area2D
func on_body_entered(body):
animation_player.play(“flying”)
but ı think ı kinda realise what ım missing, but to make sure can you check if my mistake is what ı think it is?
Is the on_body_entered triggering? Put a print or print_debug there to make sure.
ı did and its not, but ı think the problem is because ı didn’t put the animation player variable
You don’t need to set a variable - you can call the animation directly.
$AnimatedSprite2D.play("walk_right")
Try dragging and dropping the animation node in to your script to check the naming/reference/etc.
okay ıll try it rq
ı checked it and no its not working…
Is the animation in the same scene? If it’s in a different scene it’s harder to get working.
yea, ı took an animatiblebody2D from another scene and then added area2Dand animationplayer once ı put it into the main scene
This is how I triggered an animation from another scene. $death_fade is the scene which plays a fade to black. Hoping this makes sense.
@onready var SceneTranAnim = $death_fade/AnimationPlayer
print("!!YOU DEAD FROM ALPACA!!")
SceneTranAnim.play("death_fade")
await get_tree().create_timer(2).timeout
self.queue_free()
get_tree().change_scene_to_file("res://scenes/dead_from_alpaca.tscn")
but the things is that the animation isn’t from from another scene
Keep working on it. You’ll get there!
Is the debug or terminal showing an error?
Maybe another animation is taking over after the first frame…
Also - can you post more of your coding? Make sure you use the Preformatted Text option when you do.
get a reference to the animation player then on_body_entered / area entered just do you're_anim_ref.play("you're animation")
extends Area2D
@onready var animation_player = $"../AnimationPlayer"
func on_body_entered(body):
animation_player.play("flying")
print("ım flyingggg") # this part is just to get feedback if it works
already tryied, didn’t work for some reason
check if the animation_player path is correct the signal is connected and if the “flying” animation exists
I DİD İT
ı think the problem was that ı didn’t give the area2D a variable, cause when ı gave it a variable it worked. thanks for all the help
Can you share your code? I’d love to see how you made it work.
extends Area2D
@onready var area_2d = $“.”
@onready var animation_player = $“…/AnimationPlayer”
func _on_body_entered(body):
animation_player.play(“flying”)
queue_free() # this is to delete the area2D so the animation doesn’t resets
I’m not sure you need the @onready - just var by itself should work
And I don’t think you need this line - try commenting it out and see if it still works.
@onready var area_2d = $“.”