Truble resizeing an CollitionShape2D

Godot Version

4.2.2

Question

Hello friends,

I would like some help.
I’m working on a platformer where you can resize the platforms in the level in real-time, but I can’t get it to work. I can make the sprite resize with no problem, but the CollisionShape doesn’t do anything. It is a child of a RigidBody, if that helps.

extends RigidBody2D

@export var up = true
@export var down = true
@export var left = true
@export var right = true

@export_range(0, 10, 0.1) var scale_speed

var x
var y

var shape = $CollisionShape2D.shape.extents

func _ready():
    pass

func _process(delta):
    if Input.is_action_pressed("ui_up"):
        scale.y += scale_speed * delta
        y += scale_speed * delta
    if Input.is_action_pressed("ui_down") and scale.y > 0.5:
        scale.y -= scale_speed * delta
        y -= scale_speed * delta
    if Input.is_action_pressed("ui_right"):
        scale.x += scale_speed * delta
        x += scale_speed * delta
    if Input.is_action_pressed("ui_left") and scale.x > 0.5:
        scale.x -= scale_speed * delta
        x -= scale_speed * delta
    
    shape.size = Vector2(x, y)
    $CollisionShape2D.set_shape(shape)

This line is wrong var shape = $CollisionShape2D.shape.extents The variable will be initialized before the node has access to the scene tree and it won’t find the node. You should get an error in the script and a crash when trying to run the game. Either way, no Shape2D has a extents property so it will fail.

Change that line to @onready var shape = $CollisionShape2D.shape

Remove the line $CollisionShape2D.set_shape(shape) You don’t need to assign the shape again to the collision shape. Modifying its size should be enough.

Don’t scale physics bodies as it’s not supported and will cause issues. If you need to scale anything, scale the visual part of the RigidBody2D like a Sprite2D

Thank you for the reply.
Is there no way of doing this? I’m participating in my first game jam, and this was the main mechanic. If I can’t make this work, then I’ll have to start over.

Just change the size of the RectangleShape2D and scale the sprite of your rigid body. That should work