fix aiming on elevation
This commit is contained in:
parent
2116490e1f
commit
113a20589f
@ -2,7 +2,7 @@
|
||||
|
||||
[ext_resource type="Script" path="res://scripts/player.gd" id="1_xt3i8"]
|
||||
|
||||
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_14v7g"]
|
||||
[sub_resource type="CylinderShape3D" id="CylinderShape3D_apl1i"]
|
||||
height = 1.8
|
||||
|
||||
[sub_resource type="CapsuleMesh" id="CapsuleMesh_dtg5r"]
|
||||
@ -16,7 +16,7 @@ script = ExtResource("1_xt3i8")
|
||||
|
||||
[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.9, 0)
|
||||
shape = SubResource("CapsuleShape3D_14v7g")
|
||||
shape = SubResource("CylinderShape3D_apl1i")
|
||||
|
||||
[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
|
||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.9, 0)
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
[gd_scene load_steps=6 format=3 uid="uid://c0buetf2h266d"]
|
||||
[gd_scene load_steps=5 format=3 uid="uid://c0buetf2h266d"]
|
||||
|
||||
[ext_resource type="Material" uid="uid://btpy4dp5lb8il" path="res://resources/materials/test_triplanar.tres" id="1_ixaua"]
|
||||
[ext_resource type="Script" path="res://scripts/camera/following_camera.gd" id="2_7j742"]
|
||||
[ext_resource type="PackedScene" uid="uid://b73y71y3efmv" path="res://scenes/player.tscn" id="2_f4ehn"]
|
||||
[ext_resource type="Script" path="res://scripts/camera/main_camera.gd" id="3_f4hhh"]
|
||||
|
||||
@ -10,8 +9,6 @@
|
||||
[node name="Test" type="Node"]
|
||||
|
||||
[node name="Player" parent="." instance=ExtResource("2_f4ehn")]
|
||||
process_priority = 1
|
||||
process_physics_priority = 1
|
||||
|
||||
[node name="Geometry" type="Node" parent="."]
|
||||
|
||||
@ -28,22 +25,14 @@ size = Vector3(50, 1, 50)
|
||||
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="Lights"]
|
||||
transform = Transform3D(0.994881, 0.0858869, -0.0532522, 0, 0.526956, 0.849893, 0.101056, -0.845542, 0.524258, 0, 0, 0)
|
||||
shadow_enabled = true
|
||||
|
||||
[node name="Cameras" type="Node" parent="."]
|
||||
|
||||
[node name="FollowingCamera" type="Camera3D" parent="Cameras"]
|
||||
fov = 20.0
|
||||
near = 35.0
|
||||
far = 100.0
|
||||
script = ExtResource("2_7j742")
|
||||
height_offset = 0.5
|
||||
distance = 50.0
|
||||
angle_degrees = Vector3(-35, -45, 0)
|
||||
shadow_blur = 6.0
|
||||
|
||||
[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
|
||||
environment = SubResource("Environment_wctar")
|
||||
|
||||
[node name="MainCamera" type="Camera3D" parent="." node_paths=PackedStringArray("current_camera")]
|
||||
[node name="MainCamera" type="Camera3D" parent="."]
|
||||
transform = Transform3D(0.866025, -0.321394, 0.383022, 0, 0.766044, 0.642788, -0.5, -0.55667, 0.663414, 4.894, 6.43, 6.963)
|
||||
current = true
|
||||
fov = 20.0
|
||||
near = 35.0
|
||||
script = ExtResource("3_f4hhh")
|
||||
current_camera = NodePath("../Cameras/FollowingCamera")
|
||||
|
||||
@ -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:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user