How to adjust size of CollisionShape2D to match a BoxContainer size

Godot Version

Godot4

Question

`Hey there, I’m breaking my head trying to do something, for context I’m sharing my enemy’s scene tree here:

Area2D(RootNode)
↳ LabelContainer(BoxContainerNode)
↳ Label(RichTextNode)
↳ CollisionShape2D (Rectangle)

The enemy is a RichTextNode (A sentence), every time it gets a hit from a bullet its visible_ratio property decreases; until all characters are gone; this causes the shrinking of the LabelContainer node as well

The struggles I’m having are two:

  • First, the CollisionShape stays the same size, thus, the player and the bullets end up hitting something invisible as the RichTextNode, and the LabelContainer become smaller – I’ve managed to reduce the size of the CollisionShape2D but never match the same size of the other two.

  • Second the CollisionShape2D shrinks like it has the anchor point in the center… while the LabelContainer reduces it’s size from the left top corner, so the CollisionShape2D never matches the same position of the other two.

Sharing he code here, referencing each node:

@onready var label : RichTextLabel = %Label
@onready var collision_shape : CollisionShape2D = %CollisionShape2D
@onready var label_container : BoxContainer = %LabelContainer
func damage():
    set_health(health - 20)
    $AnimationPlayer.play("getting_damage")
    
    var tween := create_tween()
    var current_ratio := label.visible_ratio

    
    var current_collision_size = collision_shape.shape.extents
    var label_container_size := Vector2(label_container.get_minimum_size().x, label_container.get_minimum_size().y)
    
    if health < 100:
        tween.tween_property(label, "visible_ratio", current_ratio - 0.2, 0.2)
        var rect_coll_shape := collision_shape.shape as RectangleShape2D
        if rect_coll_shape:
            rect_coll_shape.extents = label_container_size / 2
            var new_size := label_container_size
            rect_coll_shape.extents = new_size / 2
        
    if health == 0:
        queue_free()


thanks in advance!!

Can you record a video and post it here? It would help to visualize it.

Sure, here’s the video
So, as we can see in the video, every hit the enemy get’s, the visible_ratio property becomes smaller, but, the collision shape is not…

thanks!

It seems you have a redundant assignment, which may or may not cause issues but here is what I would try:

        var rect_coll_shape := collision_shape.shape as RectangleShape2D
        if rect_coll_shape:
            # Set the collision shape extents to match the size of the label_container
            rect_coll_shape.extents = label_container_size / 2  # Adjust size based on container
            print("Updated collision shape size to: ", rect_coll_shape.extents)
        
    # When health reaches 0, free the enemy
    if health == 0:
        queue_free()

Added a Print statement too to see what comes out in the console to help debug what may be happening here

Wow! I really appreciate this!
So this was my solution

if health < 100:
		tween.tween_property(label, "visible_ratio", current_ratio - 0.2, 0.2)
		var new_size = label_container_size * current_ratio
		var rect_coll_shape := collision_shape.shape as RectangleShape2D
		if rect_coll_shape:
			rect_coll_shape.extents = label_container_size / 2.88
		
			print("update collision shape size to:", rect_coll_shape.extents)
			print("size of label container:", label_container_size)

I’m making the rect_coll_shape.extents = label_container_size / 2.88 instead 2 because for some reason, if I used 2 it never quite gets the exact same size of the label_container_size … there’s something happening, cause they never match… and notice that the label_container_size wasn’t not decreasing it’s size according to the visible_ratio property of the phrase.

So I started to play with the number to get the closer I could get, and 2.88 worked just close enough.

Attaching some videos in here, this is what happened with dividing by two

And this is how it works, when dividing by 2.88, it gets close enough

And what I was trying to do, is what manually I can do on the editor, by reducing the visible_ratio property manually, I can see how the boxcontainer node reduces it’s size, so, I was trying for the collision_shape to follow this behavior as well

Anyway! thanks so much, because this got me super close to what I was trying to do! I’m going to replicate it with other similar enemies, hoping it works as well!!

thanks again!!

:star_struck:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.