Gallery/rigidbody_scale.gd
2025-05-03 12:00:38 -06:00

47 lines
1.8 KiB
GDScript

@tool
extends RigidBody3D
# Store the original scale of the RigidBody3D
var original_scale: Vector3 = Vector3.ONE
# Store the original properties of the collision shape
var original_extents: Vector3 # For BoxShape3D
var original_radius: float # For SphereShape3D and CapsuleShape3D
var original_height: float # For CapsuleShape3D
func _ready():
# Save the original properties of the collision shape
if $CollisionShape3D.shape:
if $CollisionShape3D.shape is BoxShape3D:
original_extents = $CollisionShape3D.shape.extents
elif $CollisionShape3D.shape is SphereShape3D:
original_radius = $CollisionShape3D.shape.radius
elif $CollisionShape3D.shape is CapsuleShape3D:
original_radius = $CollisionShape3D.shape.radius
original_height = $CollisionShape3D.shape.height
func _process(delta):
if Engine.is_editor_hint(): # Only run in the editor
if scale != original_scale:
# Apply the scale to the MeshInstance3D
$MeshInstance3D.scale = scale
# Apply the scale to the CollisionShape3D
if $CollisionShape3D.shape:
var average_scale = (scale.x + scale.y + scale.z) / 3.0 # Uniform scaling
if $CollisionShape3D.shape is BoxShape3D:
# Scale the extents of the BoxShape3D
$CollisionShape3D.shape.extents = original_extents * average_scale
elif $CollisionShape3D.shape is SphereShape3D:
# Scale the radius of the SphereShape3D
$CollisionShape3D.shape.radius = original_radius * average_scale
elif $CollisionShape3D.shape is CapsuleShape3D:
# Scale the radius and height of the CapsuleShape3D
$CollisionShape3D.shape.radius = original_radius * average_scale
$CollisionShape3D.shape.height = original_height * average_scale
# Reset the RigidBody3D's scale to avoid physics issues
scale = Vector3.ONE
original_scale = scale