35 lines
845 B
GDScript
35 lines
845 B
GDScript
extends Area3D
|
|
class_name HovercraftProjectile
|
|
## Simple bullet that travels forward and destroys itself on hit or timeout.
|
|
|
|
@export var speed: float = 80.0
|
|
@export var lifetime: float = 3.0
|
|
@export var damage: float = 10.0
|
|
|
|
var _direction: Vector3 = Vector3.FORWARD
|
|
var _age: float = 0.0
|
|
|
|
|
|
func _ready() -> void:
|
|
# Use the node's forward direction if no explicit direction was set
|
|
if _direction == Vector3.FORWARD:
|
|
_direction = - global_transform.basis.z
|
|
|
|
body_entered.connect(_on_body_entered)
|
|
|
|
|
|
func set_direction(dir: Vector3) -> void:
|
|
_direction = dir.normalized()
|
|
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
global_position += _direction * speed * delta
|
|
_age += delta
|
|
if _age >= lifetime:
|
|
queue_free()
|
|
|
|
|
|
func _on_body_entered(body: Node3D) -> void:
|
|
if body.has_method("take_damage"):
|
|
body.take_damage(damage)
|
|
queue_free() |