I found your question because I wanted to dynamically scale my CollisionPolygon2D
at runtime. After being disappointed that there was no "easy" solution I could find, I thought about it for a while and realized that resizing a CollisionPolygon2D
via script is a relatively simple process under the following conditions:
- You want to uniformly scale each vertex.
- The scale is relative to the origin (0,0) of the polygon.
Here's what I came up with:
# how much to scale each vertex by
var scaleAmount : float = 2
# reference to the PoolVector2Array on the CollisionPolygon2D
var polygon : PoolVector2Array = $CollisionPolygon2D.polygon
# scale each vertex
for i in polygon.size():
polygon.set(i, polygon[i] * scaleAmount)
# assign the newly scaled vertices to the CollisionPolygon2D
$CollisionPolygon2D.polygon = polygon
I'm inexperienced with Godot and GDScript, so there are probably better ways to do it. Nonetheless, the above code could probably be modified to work as an editor tool.