3D tutorial with brownian-ish motion enemies
This commit is contained in:
40
3d-tutorial/3d_squash_the_creeps_starter/mob.gd
Normal file
40
3d-tutorial/3d_squash_the_creeps_starter/mob.gd
Normal file
@@ -0,0 +1,40 @@
|
||||
extends CharacterBody3D
|
||||
|
||||
signal squashed
|
||||
|
||||
@export var min_speed = 10
|
||||
@export var max_speed = 18
|
||||
|
||||
# In code timer for brownian motion
|
||||
var T: float = 0.5
|
||||
var Tinit: float = 1.0
|
||||
var dt: float = 0.05
|
||||
var t: float = 0.0
|
||||
|
||||
func _physics_process(_delta):
|
||||
if t > T and Tinit < 0:
|
||||
t = 0
|
||||
set_new_velocity()
|
||||
t += dt
|
||||
Tinit -= dt
|
||||
|
||||
move_and_slide()
|
||||
|
||||
func initialize(start_position, player_position):
|
||||
look_at_from_position(start_position, player_position, Vector3.UP)
|
||||
|
||||
set_new_velocity()
|
||||
|
||||
func set_new_velocity() -> void:
|
||||
rotate_y(randf_range(-PI / 4, PI / 4))
|
||||
var random_speed = randi_range(min_speed, max_speed)
|
||||
velocity = Vector3.FORWARD * random_speed
|
||||
velocity = velocity.rotated(Vector3.UP, rotation.y)
|
||||
|
||||
func _on_visible_on_screen_notifier_3d_screen_exited() -> void:
|
||||
queue_free()
|
||||
|
||||
|
||||
func squash():
|
||||
squashed.emit()
|
||||
queue_free()
|
||||
Reference in New Issue
Block a user