Files
fps_project_1/scripts/breakable.gd

35 lines
1.0 KiB
GDScript

extends RigidbodyGeneric
@export var broken_object : Resource
@export var break_velocity : float = 10
var break_on_land : bool = false
var held_currently : bool = false
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
if linear_velocity.length() >= break_velocity and !held_currently:
break_on_land = true
func breaking(current_velocity):
var spawn_broken = broken_object.instantiate()
spawn_broken.position = global_position
spawn_broken.transform.basis = global_transform.basis
spawn_broken.rotation = rotation
var pieces = spawn_broken.get_children()
for piece in pieces:
if piece is RigidBody3D:
piece.linear_velocity += current_velocity
get_tree().get_root().add_child(spawn_broken)
queue_free()
func _on_body_shape_entered(body_rid: RID, body: Node, body_shape_index: int, local_shape_index: int) -> void:
if break_on_land:
breaking(linear_velocity)