Более менее пашет
This commit is contained in:
1 parent
eed6b79efc
commit
33168b98d1
26 files changed
+443071
-19380
No files matched your search
@@ -6,14 +6,37 @@ public class PlayerLook : MonoBehaviour
|
||||
[SerializeField] private float mouseSensitivity = 150f;
|
||||
[SerializeField] private Transform playerBody;
|
||||
|
||||
[Header("Zoom Settings")]
|
||||
[SerializeField] private KeyCode zoomKey = KeyCode.Z;
|
||||
[SerializeField] private bool allowRightClickZoomWhenHandsEmpty = true;
|
||||
[SerializeField] private float zoomFov = 28f;
|
||||
[SerializeField] private float minZoomFov = 18f;
|
||||
[SerializeField] private float maxZoomFov = 45f;
|
||||
[SerializeField] private float zoomSpeed = 12f;
|
||||
[SerializeField] private float scrollZoomStep = 4f;
|
||||
[Range(0.15f, 1f)]
|
||||
[SerializeField] private float zoomSensitivityMultiplier = 0.45f;
|
||||
|
||||
private float xRotation = 0f;
|
||||
private Camera cam;
|
||||
private PlayerHands hands;
|
||||
private float defaultFov = 60f;
|
||||
private float currentZoomFov;
|
||||
|
||||
public float Sensitivity => mouseSensitivity;
|
||||
public bool IsZooming { get; private set; }
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (playerBody == null) playerBody = transform.parent != null ? transform.parent : transform;
|
||||
mouseSensitivity = PlayerPrefs.GetFloat("perekup_sensitivity", mouseSensitivity);
|
||||
|
||||
cam = GetComponent<Camera>();
|
||||
if (cam == null) cam = GetComponentInChildren<Camera>();
|
||||
if (cam != null) defaultFov = cam.fieldOfView;
|
||||
|
||||
currentZoomFov = Mathf.Clamp(zoomFov, minZoomFov, maxZoomFov);
|
||||
hands = GetComponentInParent<PlayerHands>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
@@ -22,6 +45,34 @@ public class PlayerLook : MonoBehaviour
|
||||
GameInputLock.ApplyCursor();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (cam == null) return;
|
||||
|
||||
if (hands == null) hands = GetComponentInParent<PlayerHands>();
|
||||
|
||||
bool canUseZoom = !GameInputLock.IsLocked && VehicleInteraction.Current == null;
|
||||
bool handsEmpty = hands == null || !hands.IsHoldingAnything;
|
||||
|
||||
bool keyHeld = Input.GetKey(zoomKey);
|
||||
bool rmbHeld = allowRightClickZoomWhenHandsEmpty && handsEmpty && Input.GetMouseButton(1);
|
||||
|
||||
IsZooming = canUseZoom && (keyHeld || rmbHeld);
|
||||
|
||||
// Колёсико мыши во время зума подстраивает приближение
|
||||
if (IsZooming)
|
||||
{
|
||||
float scroll = Input.mouseScrollDelta.y;
|
||||
if (Mathf.Abs(scroll) > 0.01f)
|
||||
{
|
||||
currentZoomFov = Mathf.Clamp(currentZoomFov - scroll * scrollZoomStep, minZoomFov, maxZoomFov);
|
||||
}
|
||||
}
|
||||
|
||||
float targetFov = IsZooming ? currentZoomFov : defaultFov;
|
||||
cam.fieldOfView = Mathf.Lerp(cam.fieldOfView, targetFov, Time.deltaTime * zoomSpeed);
|
||||
}
|
||||
|
||||
public void SetSensitivity(float value)
|
||||
{
|
||||
mouseSensitivity = Mathf.Clamp(value, 20f, 600f);
|
||||
@@ -37,8 +88,16 @@ public class PlayerLook : MonoBehaviour
|
||||
|
||||
public void ProcessLook(Vector2 input)
|
||||
{
|
||||
float mouseX = input.x * mouseSensitivity * Time.deltaTime;
|
||||
float mouseY = input.y * mouseSensitivity * Time.deltaTime;
|
||||
// Плавно снижаем чувствительность при приближении, чтобы удобно целиться в мелкие детали
|
||||
float sensMult = 1f;
|
||||
if (cam != null && defaultFov > minZoomFov + 1f)
|
||||
{
|
||||
float zoomT = Mathf.InverseLerp(defaultFov, minZoomFov, cam.fieldOfView);
|
||||
sensMult = Mathf.Lerp(1f, zoomSensitivityMultiplier, zoomT);
|
||||
}
|
||||
|
||||
float mouseX = input.x * mouseSensitivity * sensMult * Time.deltaTime;
|
||||
float mouseY = input.y * mouseSensitivity * sensMult * Time.deltaTime;
|
||||
|
||||
// Расчет наклона камеры с ограничением, чтобы нельзя было сломать шею
|
||||
xRotation -= mouseY;
|
||||
@@ -50,4 +109,4 @@ public class PlayerLook : MonoBehaviour
|
||||
// Вращаем само тело игрока (влево/вправо)
|
||||
playerBody.Rotate(Vector3.up * mouseX);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user