Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
FPSControl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
unity
FPSControl
Commits
3cdec643
Commit
3cdec643
authored
4 years ago
by
David Huss
Browse files
Options
Downloads
Patches
Plain Diff
Fix initial orientation issues
parent
a7894185
Branches
master
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
SmoothMouseLook.cs
+35
-10
35 additions, 10 deletions
SmoothMouseLook.cs
with
35 additions
and
10 deletions
SmoothMouseLook.cs
+
35
−
10
View file @
3cdec643
...
@@ -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
.
GetAxis
Raw
(
"Mouse X"
),
Input
.
GetAxis
Raw
(
"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
;
}
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment