godot_projects/puzzle/player_camera.gd

26 lines
731 B
GDScript

extends Camera3D
# referene to the player
@export var player : Node3D
@export var player_distance : float = 30
@export var camera_height: float = 10
@export var camera_speed: float = 10
# 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):
delta
# process position
var diff = player.position - self.position + Vector3(0, camera_height, 0)
# if the player is too far away, move the camera
if diff.length() > player_distance:
self.position += diff.normalized() * move_toward(0, diff.length() - player_distance, camera_speed) * delta
look_at(player.position)