Last active
September 8, 2017 09:44
-
-
Save Maxstupo/c0c0e7004ff28860df00dee811d4ab96 to your computer and use it in GitHub Desktop.
Allows the mouse to rotate two game object transforms.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
/* ******************************************************************************************** | |
* Program: MouseLook.cs | |
* Purpose: Allows the mouse to rotate two game object transforms. | |
* Date: 3/04/2017 | |
* Author: Maxstupo | |
* ****************************************************************************************** */ | |
public class MouseLook : MonoBehaviour { | |
public Transform yAxisTransform; | |
[Header("Input")] | |
public String xAxisInput = "Mouse X"; | |
public float xSensitivity = 2; | |
public String yAxisInput = "Mouse Y"; | |
public float ySensitivity = 2; | |
[Header("Clamping")] | |
public bool clampVerticalRotation = true; | |
[Range(-120, 120)] | |
public float minimumX = -90; | |
[Range(-120, 120)] | |
public float maximumX = 90; | |
[Header("Smoothing")] | |
public bool interpolate = true; | |
public float interpolateAmount = 20f; | |
[Header("Misc")] | |
public bool lockCursor = true; | |
private Quaternion characterTargetRot; | |
private Quaternion cameraTargetRot; | |
void Start() { | |
if (yAxisTransform == null) | |
Debug.LogWarning("MouseLook yAxisTransform is null! Disabling y-axis rotation!"); | |
characterTargetRot = transform.localRotation; | |
if (yAxisTransform != null) | |
cameraTargetRot = yAxisTransform.localRotation; | |
if (lockCursor) | |
Cursor.lockState = CursorLockMode.Locked; | |
} | |
private void Update() { | |
if (lockCursor && Cursor.lockState != CursorLockMode.Locked) { | |
if (Input.GetMouseButtonDown(0)) { | |
Cursor.lockState = CursorLockMode.Locked; | |
} else { | |
return; | |
} | |
} | |
// Avoids the mouse looking if the game is effectively paused | |
if (Mathf.Abs(Time.timeScale) < float.Epsilon) | |
return; | |
float yRot = Input.GetAxis(xAxisInput) * xSensitivity; | |
float xRot = Input.GetAxis(yAxisInput) * ySensitivity; | |
if (yAxisTransform != null) | |
cameraTargetRot *= Quaternion.Euler(-xRot, 0, 0f); | |
characterTargetRot *= Quaternion.Euler(0, yRot, 0); | |
if (clampVerticalRotation) | |
cameraTargetRot = ClampRotationAroundXAxis(cameraTargetRot); | |
if (interpolate) { | |
transform.localRotation = Quaternion.Slerp(transform.localRotation, characterTargetRot, interpolateAmount * Time.deltaTime); | |
if (yAxisTransform != null) | |
yAxisTransform.localRotation = Quaternion.Slerp(yAxisTransform.localRotation, cameraTargetRot, interpolateAmount * Time.deltaTime); | |
} else { | |
transform.localRotation = characterTargetRot; | |
if (yAxisTransform != null) | |
yAxisTransform.localRotation = cameraTargetRot; | |
} | |
} | |
private Quaternion ClampRotationAroundXAxis(Quaternion q) { | |
q.x /= q.w; | |
q.y /= q.w; | |
q.z /= q.w; | |
q.w = 1.0f; | |
float angleX = 2.0f * Mathf.Rad2Deg * Mathf.Atan(q.x); | |
angleX = Mathf.Clamp(angleX, minimumX, maximumX); | |
q.x = Mathf.Tan(0.5f * Mathf.Deg2Rad * angleX); | |
return q; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment