Lower mouse sensitivity on zoom

Lecture 177
This commit is contained in:
Stedd 2022-10-24 20:54:44 +02:00
parent e6ad4a32d7
commit 2dbd212481
2 changed files with 27 additions and 12 deletions

View File

@ -1,3 +1,5 @@
using System;
using Unity.VisualScripting.Dependencies.NCalc;
using UnityEngine; using UnityEngine;
#if ENABLE_INPUT_SYSTEM && STARTER_ASSETS_PACKAGES_CHECKED #if ENABLE_INPUT_SYSTEM && STARTER_ASSETS_PACKAGES_CHECKED
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
@ -14,12 +16,26 @@ namespace StarterAssets
public bool _sprint; public bool _sprint;
[Header("Movement Settings")] [Header("Movement Settings")]
[SerializeField] private float _mouseScale;
public bool _analogMovement; public bool _analogMovement;
[Header("Mouse Cursor Settings")] [Header("Mouse Cursor Settings")]
public bool _cursorLocked = true; public bool _cursorLocked = true;
public bool _cursorInputForLook = true; public bool _cursorInputForLook = true;
public float MouseScale
{
get => _mouseScale;
set => _mouseScale = value;
}
private void OnEnable()
{
_mouseScale = 1;
_cursorLocked = true;
SetCursorState();
}
#if ENABLE_INPUT_SYSTEM && STARTER_ASSETS_PACKAGES_CHECKED #if ENABLE_INPUT_SYSTEM && STARTER_ASSETS_PACKAGES_CHECKED
public void OnMove(InputValue value) public void OnMove(InputValue value)
{ {
@ -32,7 +48,7 @@ namespace StarterAssets
{ {
if (Time.timeScale > 0) if (Time.timeScale > 0)
{ {
LookInput(value.Get<Vector2>()); LookInput(_mouseScale * value.Get<Vector2>());
} }
else else
{ {
@ -58,12 +74,6 @@ namespace StarterAssets
} }
#endif #endif
private void OnEnable()
{
_cursorLocked = true;
SetCursorState();
}
public void MoveInput(Vector2 newMoveDirection) public void MoveInput(Vector2 newMoveDirection)
{ {
_move = newMoveDirection; _move = newMoveDirection;

View File

@ -1,5 +1,6 @@
using System; using System;
using Cinemachine; using Cinemachine;
using StarterAssets;
using Unity.VisualScripting; using Unity.VisualScripting;
using UnityEngine; using UnityEngine;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
@ -9,13 +10,17 @@ public class WeaponZoom : MonoBehaviour
{ {
[Header("Configuration")] [Header("Configuration")]
[SerializeField] private float _fovNormal; [SerializeField] private float _fovNormal;
[SerializeField] private float _senseNormal;
[SerializeField] private float _fovZoom; [SerializeField] private float _fovZoom;
[SerializeField] private float _senseZoom;
[SerializeField] private StarterAssetsInputs _input;
[Header("State")] [Header("State")]
[SerializeField] private CinemachineVirtualCamera _camera; [SerializeField] private CinemachineVirtualCamera _camera;
[SerializeField] private bool _zoomedIn; [SerializeField] private bool _zoomedIn;
private void OnEnable() private void OnEnable()
{ {
_input = GetComponent<StarterAssetsInputs>();
_camera = FindObjectOfType<CinemachineVirtualCamera>(); _camera = FindObjectOfType<CinemachineVirtualCamera>();
Debug.Assert(_camera != null, nameof(_camera) + " != null"); Debug.Assert(_camera != null, nameof(_camera) + " != null");
_fovNormal = _camera.m_Lens.FieldOfView; _fovNormal = _camera.m_Lens.FieldOfView;
@ -23,18 +28,18 @@ public class WeaponZoom : MonoBehaviour
private void Update() private void Update()
{ {
if (Mouse.current.rightButton.wasPressedThisFrame) if (!Mouse.current.rightButton.wasPressedThisFrame) return;
{
_zoomedIn = !_zoomedIn; _zoomedIn = !_zoomedIn;
}
if (_zoomedIn) if (_zoomedIn)
{ {
_input.MouseScale = _senseZoom;
_camera.m_Lens.FieldOfView = _fovZoom; _camera.m_Lens.FieldOfView = _fovZoom;
} }
else else
{ {
_input.MouseScale = _senseNormal;
_camera.m_Lens.FieldOfView = _fovNormal; _camera.m_Lens.FieldOfView = _fovNormal;
} }
} }
} }