I am creating a basic little fighting game test build, however, I am recreating this from my previous work in Unity so I am mostly trying to do the same thing here but with no success. The biggest hiccup right now is that I want to create and remove HitBox nodes on demand. So, when a player throws a punch, the game should create a new Hitbox node (extending Area2D) that contains a CollisionShape2D with specific sizing to the punch. Enable it for the Active period, then disable it and remove it upon the move ending. What is the best way to do this from code?
To address potential feedback I do not want to resize premade child objects, I want to create them on demand in code or the animation tab, but preferably with code.
class_name HitBox
extends Area2D
var collisionShape = CollisionShape2D.new()
var collisionShapeType = CapsuleShape2D.new()
func _ready():
collision_layer = 2
collision_mask = 0
monitorable = true
add_child(collisionShape)
collisionShape.name = "CollisionShape2D"
collisionShape.set_shape(collisionShapeType)
The final line only seems to add a reference to a capsule collider to the shape variable of CollisionShape2D and not an actual capsuleshape. I also tried manually setting the shape field and creating the capsule in place, all of them throw an error or dont set the shape. I am trying to get it to effectively do the same thing you do when selecting shape from the dropdown menu, but in code during runtime.