
I'm currently making a top-down shooter game where the player must survive waves of zombies.
There's a problem with the spawn. When spawned, they will rotate and move according to the direction. The problem is that they might go outside the screen.
I want them to go inside the screen.
Actor.gd
class_name Actor
extends KinematicBody2D
export var health: float
export var speed: float
var velocity: Vector2
func hit() -> void:
pass
func die() -> void:
pass
Zombie.gd
extends Actor
var direction: Vector2
func _ready():
direction = Vector2(rand_range(-1, 1), rand_range(-1, 1)).normalized()
rotation = direction.angle()
func _physics_process(delta: float) -> void:
velocity = direction * speed
velocity = move_and_slide(velocity)
func hit() -> void:
if health <= 0:
die()
func die() -> void:
queue_free()
func _on_VisibilityEnabler2D_screen_exited() -> void:
queue_free()