test場景設置
This commit is contained in:
1 parent
ec62e205b7
commit
50c43661c5
256 files changed
+13212
-144
No files matched your search
@@ -0,0 +1,111 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
// 第一人稱視角 : 掛在 Player 底下的 CameraRoot 上,Main Camera 放在 CameraRoot 底下
|
||||
// 靈敏度與 FOV 讀取 StartUI 存的 GameSettings
|
||||
public class PlayerLook : MonoBehaviour
|
||||
{
|
||||
[Header("參照")]
|
||||
[Tooltip("Main Camera")]
|
||||
public Camera targetCamera;
|
||||
|
||||
[Tooltip("Player 身上的 CapsuleCollider,用來讓視角高度跟著站/蹲改變")]
|
||||
public CapsuleCollider bodyCapsule;
|
||||
|
||||
[Header("視角高度")]
|
||||
[Tooltip("眼睛距離膠囊頂端的距離 (m)")]
|
||||
public float eyeOffsetFromTop = 0.1f;
|
||||
|
||||
[Tooltip("站/蹲切換時視角高度的跟隨速度,越大越快")]
|
||||
public float eyeHeightSpeed = 12f;
|
||||
|
||||
[Header("視角限制")]
|
||||
public float maxPitch = 89f;
|
||||
|
||||
// 與 Apex / Source 引擎相同 : 每 1 單位滑鼠位移轉 0.022° × 靈敏度
|
||||
private const float DegreesPerCount = 0.022f;
|
||||
|
||||
// Apex 的 FOV 是以 4:3 畫面為基準的水平 FOV
|
||||
private const float FovBaseAspect = 4f / 3f;
|
||||
|
||||
private float sensitivity = 1f;
|
||||
private float fovSetting = 90f;
|
||||
private float yaw;
|
||||
private float pitch;
|
||||
private float lastAspect;
|
||||
|
||||
// ===== 給輔助腳本用的接口 =====
|
||||
|
||||
// 平滑跟槍 : 這一幀要額外加上的滑鼠位移 (與滑鼠同單位),用完會歸零
|
||||
private Vector2 pendingAssistDelta;
|
||||
|
||||
// 動態靈敏度 : 乘在靈敏度上的倍率,1 = 不變
|
||||
[HideInInspector] public float sensitivityScale = 1f;
|
||||
|
||||
public void AddAssistDelta(Vector2 delta)
|
||||
{
|
||||
pendingAssistDelta += delta;
|
||||
}
|
||||
|
||||
// =============================
|
||||
|
||||
void Start()
|
||||
{
|
||||
GameSettings settings = GameSettings.Load();
|
||||
sensitivity = settings.sensitivity;
|
||||
fovSetting = settings.fov;
|
||||
|
||||
yaw = transform.eulerAngles.y;
|
||||
pitch = 0f;
|
||||
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
|
||||
ApplyFov();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
Vector2 mouseDelta = Mouse.current != null ? Mouse.current.delta.ReadValue() : Vector2.zero;
|
||||
|
||||
// 玩家輸入套用 (可被動態靈敏度改變) + 輔助位移
|
||||
Vector2 totalDelta = mouseDelta * sensitivityScale + pendingAssistDelta;
|
||||
pendingAssistDelta = Vector2.zero;
|
||||
|
||||
float degreesPerCount = DegreesPerCount * sensitivity;
|
||||
yaw += totalDelta.x * degreesPerCount;
|
||||
pitch -= totalDelta.y * degreesPerCount; // 滑鼠往上 = 抬頭
|
||||
pitch = Mathf.Clamp(pitch, -maxPitch, maxPitch);
|
||||
|
||||
transform.rotation = Quaternion.Euler(pitch, yaw, 0f);
|
||||
|
||||
UpdateEyeHeight();
|
||||
|
||||
// 視窗比例改變時重新計算 FOV
|
||||
if (targetCamera != null && !Mathf.Approximately(targetCamera.aspect, lastAspect))
|
||||
{
|
||||
ApplyFov();
|
||||
}
|
||||
}
|
||||
|
||||
void UpdateEyeHeight()
|
||||
{
|
||||
if (bodyCapsule == null) return;
|
||||
|
||||
float targetY = bodyCapsule.center.y + bodyCapsule.height * 0.5f - eyeOffsetFromTop;
|
||||
Vector3 pos = transform.localPosition;
|
||||
pos.y = Mathf.Lerp(pos.y, targetY, 1f - Mathf.Exp(-eyeHeightSpeed * Time.deltaTime));
|
||||
transform.localPosition = pos;
|
||||
}
|
||||
|
||||
// 取目前畫面能容下的最大 4:3 區域,FOV 設定值就是這塊區域的水平 FOV
|
||||
// 例 : 1920×1080 → 中間的 1440×1080 ; 比 4:3 窄的畫面 → 整個寬度
|
||||
void ApplyFov()
|
||||
{
|
||||
if (targetCamera == null) return;
|
||||
|
||||
lastAspect = targetCamera.aspect;
|
||||
float aspect = Mathf.Min(lastAspect, FovBaseAspect);
|
||||
targetCamera.fieldOfView = Camera.HorizontalToVerticalFieldOfView(fovSetting, aspect);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a0e8cd6eccf4349438a7eb38773f9923
|
||||
@@ -1,5 +1,5 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 652d306d5da9f394ba03403e88093a84
|
||||
guid: 5632db3eb3d2a0e4593e1be96e85c7d5
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Reference in new issue
Block a user