Godot Version
4.4.1
Question
I’m trying scale a RigidBody2D object. I have a simple layout of a TestScene and adding a Mob (the RigidBody2D) object to it. I’m adding it via code in a PackedScene through a Timer whenever it times out. This is the only place I set scale.
func _on_timer_timeout() -> void:
var mob = mob_scene.instantiate()
mob.scale = Vector2(5, 5)
mob.position = Vector2(500, 500)
add_child(mob)
However when the monster enters, it flashes at 5X and then it immediately reverts back to 1X. Why does this happen?
More Info. Mob has 3 child nodes → Sprite2D, AnimationPlayer, and CollisionShape2D
Any help would be appreciated thanks!
RigidBodies have their scale reverted to (1, 1)
by the physics engine at run-time. You’ll get a warning if you try to scale one in the editor, but not at run-time.
One workaround would be to parent the RigidBody to a new Node2D and scale the Node2D instead. Keep in mind that the Node2D wouldn’t move with the body, though.
Another workaround would be to resize the collision shape and scale all of the body’s children instead of the body.
- Make the CollisionShape2D’s shape unique (duplicate it)
- Resize the shape by 5X
- Scale each other child by 5X
- Multiply the position of each child by 5
1 Like
ooof. thanks for the info.
I will try the second approach. I need the RigidBody to set velocity, otherwise I would use something else. How do people normally do projectiles and monsters?
What does “Multiply the position of each child by 5” mean? Can I center my node around (0, 0) so I don’t have to deal with position adjustments?
Thanks again I appreciate your detailed response!!
You can parent the entire RigidBody to a new node, not just its children. Unless I’m missing something, physics still works identically. The only caveat is that the parent won’t follow the RigidBody anymore. You would need to keep that in mind anytime you read the body’s position.
If you’re going to scale each individual child, you’ll need to scale their positions as well. If you’re setting the scale of each child to (5, 5), you’ll need to multiply each child’s position by (5, 5).
Yes. You’ll only need to adjust nodes’ positions if they’ve been moved.
I ended up going with your second approach where I resized Sprite2D and CollisionShape2D instead. I’ve tested it a little and things seem to be working as intended when I scale in code. Thank you so much again for the insights!! 
1 Like