refactor mouse aiming
This commit is contained in:
@@ -23,7 +23,6 @@ func _ready() -> void:
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
call_deferred("_mouse_aiming")
|
||||
|
||||
_process_respawning()
|
||||
|
||||
|
||||
@@ -37,31 +36,29 @@ func _physics_process(delta: float) -> void:
|
||||
move_and_slide()
|
||||
|
||||
|
||||
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, player_pos)
|
||||
var intersection: Variant = plane.intersects_ray(from, direction)
|
||||
if not intersection:
|
||||
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
|
||||
var aim_position := _mouse_project(mouse_pos, player_position, camera)
|
||||
var aim_position := _mouse_project(_floor_height)
|
||||
aim_offset = aim_position - player_position
|
||||
aim_position.y = global_position.y
|
||||
look_at(aim_position, Vector3.UP, true)
|
||||
|
||||
|
||||
func _mouse_project(height: float) -> Vector3:
|
||||
var mouse_pos := get_viewport().get_mouse_position()
|
||||
var camera := Referencer.main_camera
|
||||
var from := camera.project_ray_origin(mouse_pos)
|
||||
var direction := camera.project_ray_normal(mouse_pos)
|
||||
var plane := Plane(Vector3.UP, height)
|
||||
var intersection: Variant = plane.intersects_ray(from, direction)
|
||||
if not intersection:
|
||||
return Vector3.ZERO
|
||||
return intersection as Vector3
|
||||
|
||||
|
||||
func _process_respawning() -> void:
|
||||
if global_position.y < _respawn_height:
|
||||
global_position = _respawn_point
|
||||
|
||||
Reference in New Issue
Block a user