The reason the new objects aren't decreasing in size is that scaling the RigidBody2D node (using new_object.scale = original_scale * 0.5) does not automatically scale its child nodes, such as the Sprite and CollisionShape2D. By default, child nodes in Godot do not inherit the scale of their parent node unless specifically set to do so.
Here's how you can fix the issue:
-
Enable Scale Inheritance for Child Nodes:
In your char.tscn scene, select the child nodes (e.g., Sprite, CollisionShape2D) and enable the "Scale" option in the Transforms section to make them inherit the scale of their parent. Here's how:
- Select the child node (e.g.,
Sprite).
- In the Inspector, scroll down to the Transform section.
- Check the Scale checkbox under Inherits Transform or Transform Affects (depending on your Godot version).
Repeat this for each child node that should scale with the parent.
-
Scale the Collision Shape Properly:
For the CollisionShape2D, simply inheriting the scale might not adjust the collision shape's size appropriately. You need to adjust the shape's size directly. If you're using a CircleShape2D, you can scale its radius. Here's how you can adjust it in your script:
func split_object():
var original_scale = self.scale
for i in range(2):
var new_object = smaller_object_scene.instantiate()
new_object.position = self.position + Vector2(randf(), randf())
new_object.scale = original_scale * 0.5
# Adjust the collision shape
var collision_shape = new_object.get_node("CollisionShape2D")
var shape = collision_shape.shape
if shape is CircleShape2D:
shape.radius *= 0.5
elif shape is RectangleShape2D:
shape.extents *= 0.5
# Add other shape types as needed
get_parent().add_child(new_object)
queue_free()
-
Ensure Sprites are Scaling:
If your Sprite or other visual nodes are not scaling, make sure they also inherit scale or adjust their scale property directly in the script:
var sprite = new_object.get_node("Sprite")
sprite.scale = original_scale * 0.5
Summary:
Your new objects aren't smaller because scaling the parent RigidBody2D doesn't automatically scale its children. To fix this, you need to:
- Enable scale inheritance on child nodes (like
Sprite and CollisionShape2D) in the editor.
- Adjust the collision shape sizes directly in your script to ensure the physics bodies are correctly scaled.
By making these changes, the new objects created by split_object() will be properly scaled down to 50% of the original size, as you intended.
Updated split_object() function:
func split_object():
var original_scale = self.scale
for i in range(2):
var new_object = smaller_object_scene.instantiate()
new_object.position = self.position + Vector2(randf(), randf())
new_object.scale = original_scale * 0.5
# Adjust the collision shape
var collision_shape = new_object.get_node("CollisionShape2D")
var shape = collision_shape.shape
if shape is CircleShape2D:
shape.radius *= 0.5
elif shape is RectangleShape2D:
shape.extents *= 0.5
# Adjust other properties if necessary
get_parent().add_child(new_object)
queue_free()
By properly scaling both the visual and physics components, your new objects will appear at half the size of the original, just as you expect.
Answer:
Because scaling the RigidBody2D node doesn’t automatically scale its child nodes (like the Sprite and CollisionShape2D), so the new objects aren’t smaller—you need to adjust those child nodes to inherit the parent’s scale or manually scale them in code.