fix aiming on elevation
This commit is contained in:
@@ -1,15 +0,0 @@
|
||||
class_name FollowingCamera
|
||||
extends Camera3D
|
||||
|
||||
@export var height_offset: float = 0.4
|
||||
@export var distance: float = 6
|
||||
@export var angle_degrees: Vector3
|
||||
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
var follow_position := (
|
||||
Referencer.player.global_position + Vector3.UP * height_offset
|
||||
)
|
||||
|
||||
global_rotation_degrees = angle_degrees
|
||||
global_position = follow_position + transform.basis.z * distance
|
||||
@@ -1,7 +1,12 @@
|
||||
class_name MainCamera
|
||||
extends Camera3D
|
||||
|
||||
@export var current_camera: Camera3D
|
||||
@export var height_offset: float = 0.5
|
||||
@export var distance: float = 50
|
||||
@export var angle_degrees: Vector3 = Vector3(-35, -45, 0)
|
||||
@export var aim_offset_factor: float = 0.2
|
||||
|
||||
var _floor_height: float = 0
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
@@ -9,12 +14,18 @@ func _ready() -> void:
|
||||
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
global_position = current_camera.global_position
|
||||
global_rotation = current_camera.global_rotation
|
||||
fov = current_camera.fov
|
||||
near = current_camera.near
|
||||
far = current_camera.far
|
||||
var player_position := Referencer.player.global_position
|
||||
if Referencer.player.is_on_floor():
|
||||
_floor_height = player_position.y
|
||||
player_position.y = _floor_height
|
||||
|
||||
var mouse_pos := get_viewport().get_mouse_position()
|
||||
var aim_position := Player.mouse_project(mouse_pos, player_position, self)
|
||||
|
||||
func is_camera_current(camera: Camera3D) -> bool:
|
||||
return current_camera == camera
|
||||
var aim_relative := aim_position - player_position
|
||||
var follow_position := player_position + aim_relative * aim_offset_factor
|
||||
|
||||
global_rotation_degrees = angle_degrees
|
||||
global_position = (
|
||||
follow_position + Vector3.UP * height_offset + transform.basis.z * distance
|
||||
)
|
||||
|
||||
@@ -10,7 +10,10 @@ const FALL_ACCELERATION: float = 25
|
||||
|
||||
@export var _respawn_height: float = -5
|
||||
|
||||
var aim_position: Vector3
|
||||
|
||||
var _respawn_point: Vector3
|
||||
var _floor_height: float = 0
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
@@ -19,7 +22,9 @@ func _ready() -> void:
|
||||
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
_mouse_turning()
|
||||
_mouse_aiming()
|
||||
look_at(aim_position, Vector3.UP, true)
|
||||
|
||||
_process_respawning()
|
||||
|
||||
|
||||
@@ -27,23 +32,33 @@ func _physics_process(delta: float) -> void:
|
||||
if not is_multiplayer_authority():
|
||||
return
|
||||
|
||||
_mouse_turning()
|
||||
_lateral_movement(delta)
|
||||
_vertical_movement(delta)
|
||||
|
||||
move_and_slide()
|
||||
|
||||
|
||||
func _mouse_turning() -> void:
|
||||
var mouse_pos := get_viewport().get_mouse_position()
|
||||
var camera := Referencer.main_camera
|
||||
static func mouse_project(
|
||||
mouse_pos: Vector2, player_pos: Vector3, camera: Camera3D
|
||||
) -> Vector3:
|
||||
var from := camera.project_ray_origin(mouse_pos)
|
||||
var direction := camera.project_ray_normal(mouse_pos)
|
||||
var plane := Plane(Vector3.UP, global_position)
|
||||
var plane := Plane(Vector3.UP, player_pos)
|
||||
var intersection: Variant = plane.intersects_ray(from, direction)
|
||||
if not intersection:
|
||||
return
|
||||
look_at(intersection as Vector3, Vector3.UP, true)
|
||||
return Vector3.ZERO
|
||||
return intersection as Vector3
|
||||
|
||||
|
||||
func _mouse_aiming() -> void:
|
||||
var mouse_pos := get_viewport().get_mouse_position()
|
||||
var player_position := global_position
|
||||
if is_on_floor():
|
||||
_floor_height = player_position.y
|
||||
player_position.y = _floor_height
|
||||
var camera := Referencer.main_camera
|
||||
aim_position = mouse_project(mouse_pos, player_position, camera)
|
||||
aim_position.y = global_position.y
|
||||
|
||||
|
||||
func _process_respawning() -> void:
|
||||
|
||||
Reference in New Issue
Block a user