change camera positioning to target-based

This commit is contained in:
2024-10-05 18:21:12 +10:00
parent 19eb9473c0
commit 0a6fe86920

View File

@@ -1,26 +1,35 @@
extends Camera3D
const MOVE_SPEED: float = 2
const ZOOM_SPEED: float = 0.25
## How many pixels the mouse needs to be away
## from the screen edge to move the camera.
const EDGE_THRESHOLD: float = 10
@export var zoom_in_height: float = 1
@export var zoom_in_distance: float = 1
@export var zoom_in_fov: float = 60
@export var zoom_in_angle: float = -0.1 * PI
@export var zoom_in_speed: float = 2
@export var zoom_out_height: float = 3
@export var zoom_out_distance: float = 3
@export var zoom_out_fov: float = 70
@export var zoom_out_angle: float = -0.25 * PI
@export var zoom_out_speed: float = 4
var target_position: Vector3 = Vector3(0, 0, 0)
var mouse_position: Vector2 = Vector2()
## 0 = zoomed in. 1 = zoomed out.
var zoom_value: float = 0.25
func _process(delta: float) -> void:
handle_movement(delta)
handle_zoom()
fov = lerp(zoom_in_fov, zoom_out_fov, zoom_value)
rotation.x = lerp(zoom_in_angle, zoom_out_angle, zoom_value)
var distance: float = lerp(zoom_in_distance, zoom_out_distance, zoom_value)
var offset_direction := Vector3.BACK.rotated(Vector3.RIGHT, rotation.x)
var offset := offset_direction * distance
position = target_position + offset
func _input(event: InputEvent) -> void:
if event is InputEventMouseMotion:
@@ -61,10 +70,6 @@ func handle_movement(delta: float) -> void:
.rotated(Vector3.UP, rotation.y)
)
var velocity := direction * MOVE_SPEED
position += velocity * delta
func handle_zoom() -> void:
fov = lerp(zoom_in_fov, zoom_out_fov, zoom_value)
rotation.x = lerp(zoom_in_angle, zoom_out_angle, zoom_value)
position.y = lerp(zoom_in_height, zoom_out_height, zoom_value)
var speed: float = lerp(zoom_in_speed, zoom_out_speed, zoom_value)
var velocity := direction * speed
target_position += velocity * delta