Commit 3cdec643 authored by David Huss's avatar David Huss 💬

Fix initial orientation issues

parent a7894185
......@@ -6,6 +6,9 @@ using UnityEngine;
[AddComponentMenu("MouseLook ")]
public class SmoothMouseLook : MonoBehaviour
{
[Tooltip("Give the user control over this?")]
[SerializeField] bool enable = true;
// The speed at which we turn (Mouse Sensitivity)
// mouseSensitivity.x is for left <-> right
// mouseSensitivity.y is for up <-> down
......@@ -16,6 +19,9 @@ public class SmoothMouseLook : MonoBehaviour
[Tooltip("How smooth/mushy the mouse movement becomes (bigger=smoother, X: left<->right, Y: up<->down).")]
[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
// Must be bigger than headLowerAngleLimit
[Tooltip("How many degrees the head can look up at max.")]
......@@ -102,33 +108,52 @@ public class SmoothMouseLook : MonoBehaviour
{
// Read the Position-Change between this Frame and the last
// 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
// Vector we read before and return a smoothed one
smoothedMouseDelta = EaseMouseDelta(mouseDelta);
// Flip the vertical Control, if InvertAxisY is true
if (InvertAxisY) {
if (InvertAxisY)
{
smoothedMouseDelta.y *= -1;
}
// Add the mouse movements to the current value of yaw and pitch
yaw += smoothedMouseDelta.x;
pitch += smoothedMouseDelta.y;
if (enable) {
// Add the mouse movements to the current value of yaw and pitch
yaw += smoothedMouseDelta.x;
pitch += smoothedMouseDelta.y;
// Clamp pitch so that we can't look directly down or up
pitch = Mathf.Clamp(pitch, headLowerAngleLimit, headUpperAngleLimit);
// 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
pitch = Mathf.Clamp(pitch, headLowerAngleLimit, headUpperAngleLimit);
} else {
yaw = 0.0f;
pitch = 0.0f;
}
// Compute rotations by rotating around a fixed axis (rotate yaw-degrees
// around the up direction for the body, and pitch degrees around the
// right direction for the head).
// Note: 90 deg need to be added, to get the initial orientation right
var bodyRotation = Quaternion.AngleAxis(yaw + 90, Vector3.up);
var headRotation = Quaternion.AngleAxis(pitch, Vector3.right);
var bodyRotation = Quaternion.AngleAxis(yaw, Vector3.up);
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
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;
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment