Godot Ver.
4.2.2
Question
` Is there any way to “animate” the dimensions of an Area2D with code, or with pre-making a set of Area2D shapes to quickly change between?
The best visual example of what I’m trying to achieve would be how hit- and hurt-boxes are manipulated in fighting games. Rather than being static, or there only being one hit/hurtbox per animation, they have intricate and rapidly updated "Area2D’ designs to match sprites. Is it possible to replicate something similar with Godot? If so, how?
Any help is much appreciated, as I’m new to the engine from GM:S`
There are several ways you could do this, depending on your needs. Most simply, area2d
has monitorable
and monitoring
boolean properties you ought to be able to use on the fly to turn them on and off. So, for example, if you have a “kick attack” area2d
on a character’s foot, you could have both set to false
most of the time, and only turn them on when the character is actually kicking.
If you want to change collision shape with code you will need to get reference to its shape:
extends Node2D
@onready var my_collision : RectangleShape2D = $Area2D/CollisionShape2D.shape
func _process(delta: float) -> void:
my_collision.size.x += 0.1
In this example i am getting reference to shape
of child node located in pathArea2D/CollisionShape2D
and increase it’s x
by 0.1 each frame.
You can also use AnimationPlayer
and manipulate collision shape using key frames.
Would this allow me to create and destroy area2Ds in tune with the animations?
Similarly, would making every frame of an attack animation a key frame, and manually manipulating the area2D for every frame be reasonable?
you can disable the areas when you don’t need them. I don’t think you should create and destroy them in code.
If you prefer visual editing, then i recommend using AnimationPlayer
. You don’t need to put key frame for each frame of animation, only when you need to change it.
And as paintsimmon said, destroying and creating nodes frequently is a bad idea.
2 Likes