Files
Cielonos/Assets/OtherPlugins/HUD-Navigation-System/_Examples/ExampleScene/Scripts/ExampleMouseLook.cs

87 lines
2.2 KiB
C#
Raw Normal View History

2026-04-18 13:57:19 -04:00
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
2025-11-25 08:19:33 -05:00
namespace SickscoreGames.ExampleScene
{
public class ExampleMouseLook : MonoBehaviour
{
#region Variables
public float sensitivityX = 3f;
public float sensitivityY = 3f;
public Vector2 rotationLimitsX = new Vector2 (-360f, 360f);
public Vector2 rotationLimitsY = new Vector2 (-60f, 60f);
public float rotationSmooth = 8f;
2026-04-18 13:57:19 -04:00
private Quaternion _rotationOrigin;
private float _currentRotationX, _currentRotationY = 0f;
2025-11-25 08:19:33 -05:00
#endregion
#region Main Methods
void Awake ()
{
2026-04-18 13:57:19 -04:00
_rotationOrigin = transform.localRotation;
#if ENABLE_INPUT_SYSTEM
sensitivityX /= 4;
sensitivityY /= 4;
#endif
2025-11-25 08:19:33 -05:00
}
void Update ()
{
// get input
2026-04-18 13:57:19 -04:00
float mouseX = 0f;
float mouseY = 0f;
#if ENABLE_INPUT_SYSTEM
if (Mouse.current != null)
{
mouseX = Mouse.current.delta.x.ReadValue();
mouseY = Mouse.current.delta.y.ReadValue();
}
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
mouseX = Input.GetAxis ("Mouse X");
mouseY = Input.GetAxis ("Mouse Y");
#endif
2025-11-25 08:19:33 -05:00
// calculate and apply rotations
if (axes == RotationAxes.MouseX) {
2026-04-18 13:57:19 -04:00
_currentRotationX += mouseX * sensitivityX;
_currentRotationX = this.ClampAngle (_currentRotationX, rotationLimitsX.x, rotationLimitsX.y);
Quaternion rotationX = Quaternion.AngleAxis (_currentRotationX, Vector3.up);
transform.localRotation = Quaternion.Lerp (transform.localRotation, _rotationOrigin * rotationX, rotationSmooth * Time.deltaTime);
2025-11-25 08:19:33 -05:00
} else {
2026-04-18 13:57:19 -04:00
_currentRotationY += mouseY * sensitivityY;
_currentRotationY = this.ClampAngle (_currentRotationY, rotationLimitsY.x, rotationLimitsY.y);
Quaternion rotationY = Quaternion.AngleAxis (-_currentRotationY, Vector3.right);
transform.localRotation = Quaternion.Lerp (transform.localRotation, _rotationOrigin * rotationY, rotationSmooth * Time.deltaTime);
2025-11-25 08:19:33 -05:00
}
}
2026-04-18 13:57:19 -04:00
2025-11-25 08:19:33 -05:00
#endregion
#region Utility Methods
float ClampAngle (float angle, float min, float max)
{
angle %= 360f;
if (angle < -360f)
angle += 360f;
if (angle > 360f)
angle -= 360f;
return Mathf.Clamp (angle, min, max);
}
#endregion
#region Subclasses
public enum RotationAxes { MouseX, MouseY }
public RotationAxes axes = RotationAxes.MouseX;
#endregion
}
}