Select Git revision
SmoothMouseLook.cs
-
David Huss authoredDavid Huss authored
SmoothMouseLook.cs 5.45 KiB
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("MouseLook ")]
public class SmoothMouseLook : MonoBehaviour
{
// The speed at which we turn (Mouse Sensitivity)
// mouseSensitivity.x is for left <-> right
// mouseSensitivity.y is for up <-> down
[Tooltip("How fast to turn when moving the mouse (bigger=faster, X: left<->right, Y: up<->down).")]
[SerializeField] Vector2 mouseSensitivity = new Vector2(70f, 60f);
// How much smoothing goes on for each axis
[Tooltip("How smooth/mushy the mouse movement becomes (bigger=smoother, X: left<->right, Y: up<->down).")]
[SerializeField] Vector2 smoothing = new Vector2(2f, 2f);
// How far up the head can tilt, measured in angles from the horizon
// Must be bigger than headLowerAngleLimit
[Tooltip("How many degrees the head can look up at max.")]
[SerializeField] float headUpperAngleLimit = 85f;
// How far down the head can tilt, measured in angles from the horizon
// Must be smaller than headUpperAngleLimit
[Tooltip("How many degrees the head can look down at max.")]
[SerializeField] float headLowerAngleLimit = -80f;
// Invert the Y Axis of the Mouse if true
[Tooltip("Invert Mouse Control for up<->down.")]
[SerializeField] bool InvertAxisY = true;
// Our current rotation from start in degrees
float yaw = 0f;
float pitch = 0f;
// Stores the orientations of the head and body when the game started
// We'll derive new orientations by combining these with the variables yaw
// and pitch
Quaternion bodyStartOrientation;
Quaternion headStartOrientation;
// A reference to the head object (the object that will rotate up and down)
// The body is the current (this) object, so there is no variable needed.
// We don't want to expose this to the interface. Instead we just look for a
// Child object with type Camera, when the game starts.
Transform head;
// Two 2D-Vectors that store both axis of the mouse
private Vector2 smoothedMouseDelta;
// Start is called before the first frame update
void Start()
{
// Find the head – this returns the transform parameter of this objects
// first Child of type Camera. If none is found head = null
head = GetComponentInChildren<Camera>().transform;
// Cache the orientation of body and head. This errors if head was not
// found
bodyStartOrientation = transform.localRotation;
headStartOrientation = head.transform.localRotation;
// Lock and hide the cursor
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}