How do i change radius of CircleShape2D by script?

Godot Version

Godot Engine v4.3.stable.official.77dcf97d8 - https://godotengine.org

Question

Basically the title…

Started to learn gamedev from 0 couple of weeks ago, so i have really limited knowledge about coding and Godot itself

I’m trying to make separate explosion scene, which i will instantiate by other scripts and pass through “explosion power”, so explosion radius would be different, depending on that “power”

So far my code looks like this:

extends Area2D
class_name explosion2D

@onready var expl_shape: CollisionShape2D = $ExplosionShape

# blank variables
var base_damage : float # not used
var expl_rad : float 

func boom(
shell_expl_pow : float,
):
	print("RECIEVED shell_expl_pow: " + str(shell_expl_pow))
	expl_rad =  shell_expl_pow * 15
	self.scale = Vector2(expl_rad, expl_rad)

As you can see, currently i’m trying to use scale (CollisionShape2D’s CircleShape2D is scaled to 1px manually), but that gives unstable results somehow: https://youtu.be/Fvx_nDcXvDc

How can i change CircleShape2D by script?

I’ve tried to use radius property, but didn’t manage to wrap my head arround, how to properly use it… And the only simillar topic i found was from 2020, so probably Godot ver 3.x was used and i guess something was changed since…

Thanks in advance for any help

$ExplosionShape.shape.radius = some_value

will change the radius to that value.

Keep in mind, CollisionShapes can be circles or rectangles or whatever, and these have different properties. I prefer doing this a bit more safely:

if $ExplosionShape.shape is CircleShape2D:
    $ExplosionShape.shape.radius = some_value
1 Like

Well, thank you!

I tried same method before, but it didn’t work for me in the past, CTRL+C and CTRL+V, and this time it worked!

I found another workaround, just because “.shape.radius” didn’t work out for me in past:

func boom(
shell_expl_pow : float,
):
	expl_rad = shell_expl_pow * 5
	print("expl_rad: " + str(expl_rad))
	$ExplosionParticle.scale = Vector2(shell_expl_pow/4, shell_expl_pow/4)
	$Animation.play("Explosion")
	var newCollisionShape = CollisionShape2D.new()
	newCollisionShape.position = Vector2.ZERO
	var newCircleShape = CircleShape2D.new()
	newCircleShape.radius = expl_rad
	newCollisionShape.shape = newCircleShape
	self.add_child(newCollisionShape)

But basically, it does the same thing in my case.

P.s. “Scale” bug was caused by incorrect scale of shell itself, so now onwards i’m going to try not to scale root nodes without need :slight_smile:

That’s a lesson well learned. I’ve had many headaches that eventually traced back to messing with the scale parameter :smiley: