Skip to content
Snippets Groups Projects
Commit 3cdec643 authored by David Huss's avatar David Huss :speech_balloon:
Browse files

Fix initial orientation issues

parent a7894185
Branches master
No related tags found
No related merge requests found
...@@ -6,6 +6,9 @@ using UnityEngine; ...@@ -6,6 +6,9 @@ using UnityEngine;
[AddComponentMenu("MouseLook ")] [AddComponentMenu("MouseLook ")]
public class SmoothMouseLook : MonoBehaviour public class SmoothMouseLook : MonoBehaviour
{ {
[Tooltip("Give the user control over this?")]
[SerializeField] bool enable = true;
// The speed at which we turn (Mouse Sensitivity) // The speed at which we turn (Mouse Sensitivity)
// mouseSensitivity.x is for left <-> right // mouseSensitivity.x is for left <-> right
// mouseSensitivity.y is for up <-> down // mouseSensitivity.y is for up <-> down
...@@ -16,6 +19,9 @@ public class SmoothMouseLook : MonoBehaviour ...@@ -16,6 +19,9 @@ public class SmoothMouseLook : MonoBehaviour
[Tooltip("How smooth/mushy the mouse movement becomes (bigger=smoother, X: left<->right, Y: up<->down).")] [Tooltip("How smooth/mushy the mouse movement becomes (bigger=smoother, X: left<->right, Y: up<->down).")]
[SerializeField] Vector2 smoothing = new Vector2(2f, 2f); [SerializeField] Vector2 smoothing = new Vector2(2f, 2f);
[Tooltip("How many frames to wait full control is given to the user.")]
[SerializeField] float startupTime = 100.0f;
// How far up the head can tilt, measured in angles from the horizon // How far up the head can tilt, measured in angles from the horizon
// Must be bigger than headLowerAngleLimit // Must be bigger than headLowerAngleLimit
[Tooltip("How many degrees the head can look up at max.")] [Tooltip("How many degrees the head can look up at max.")]
...@@ -102,33 +108,52 @@ public class SmoothMouseLook : MonoBehaviour ...@@ -102,33 +108,52 @@ public class SmoothMouseLook : MonoBehaviour
{ {
// Read the Position-Change between this Frame and the last // Read the Position-Change between this Frame and the last
// Note: GetRawAxis gives more sensitivity // Note: GetRawAxis gives more sensitivity
var mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")); var mouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
// Run the Smoothing-Function we defined above on the Mouse // Run the Smoothing-Function we defined above on the Mouse
// Vector we read before and return a smoothed one // Vector we read before and return a smoothed one
smoothedMouseDelta = EaseMouseDelta(mouseDelta); smoothedMouseDelta = EaseMouseDelta(mouseDelta);
// Flip the vertical Control, if InvertAxisY is true // Flip the vertical Control, if InvertAxisY is true
if (InvertAxisY) { if (InvertAxisY)
{
smoothedMouseDelta.y *= -1; smoothedMouseDelta.y *= -1;
} }
if (enable) {
// Add the mouse movements to the current value of yaw and pitch // Add the mouse movements to the current value of yaw and pitch
yaw += smoothedMouseDelta.x; yaw += smoothedMouseDelta.x;
pitch += smoothedMouseDelta.y; pitch += smoothedMouseDelta.y;
// Scale the values initially to avoid jumps on scene startup
yaw = SoftStart(yaw);
pitch = SoftStart(pitch);
// Clamp pitch so that we can't look directly down or up // Clamp pitch so that we can't look directly down or up
pitch = Mathf.Clamp(pitch, headLowerAngleLimit, headUpperAngleLimit); pitch = Mathf.Clamp(pitch, headLowerAngleLimit, headUpperAngleLimit);
} else {
yaw = 0.0f;
pitch = 0.0f;
}
// Compute rotations by rotating around a fixed axis (rotate yaw-degrees // Compute rotations by rotating around a fixed axis (rotate yaw-degrees
// around the up direction for the body, and pitch degrees around the // around the up direction for the body, and pitch degrees around the
// right direction for the head). // right direction for the head).
// Note: 90 deg need to be added, to get the initial orientation right // Note: 90 deg need to be added, to get the initial orientation right
var bodyRotation = Quaternion.AngleAxis(yaw + 90, Vector3.up); var bodyRotation = Quaternion.AngleAxis(yaw, Vector3.up);
var headRotation = Quaternion.AngleAxis(pitch, Vector3.right); var headRotation = Quaternion.AngleAxis(pitch, Vector3.left);
// Debug.LogFormat("bodyRotation: {0}, headRotation: {1}", bodyRotation, headRotation);
// Finally combine the rotations for body and head with the start rotations // Finally combine the rotations for body and head with the start rotations
transform.localRotation = bodyRotation * bodyStartOrientation; transform.localRotation = bodyRotation * bodyStartOrientation;
head.localRotation = headRotation * headStartOrientation; head.transform.localRotation = headRotation * headStartOrientation;
}
float SoftStart(float invalue) {
if (Time.frameCount < startupTime) {
return invalue * Mathf.Lerp(0.0f, 1.0f, Time.frameCount/startupTime);
} else {
return invalue;
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment