-
Notifications
You must be signed in to change notification settings - Fork 0
/
FirstPersonCamera.cs
162 lines (137 loc) · 4.95 KB
/
FirstPersonCamera.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
using UnityEngine;
/// <summary>
/// A first-person camera controller.
/// </summary>
/// This script provides a first-person camera experience with features like mouse look,
/// head bobbing, dynamic field of view (FOV) adjustments, and camera sway. It's designed to be
/// attached to a camera object within a first-person player model.
public class FirstPersonCamera : MonoBehaviour
{
[Header("Mouse Look Settings")]
[Tooltip("Sensitivity of mouse movement.")]
public float mouseSensitivity = 100f;
[Tooltip("Reference to the player's body transform for rotation.")]
public Transform playerBody;
[Tooltip("The camera component attached to this object.")]
public Camera playerCamera;
// Internal variable to track the camera's x-axis rotation.
private float xRotation = 0f;
[Header("Head Bobbing Settings")]
[Tooltip("Speed of head bobbing.")]
public float bobbingSpeed = 0.18f;
[Tooltip("Amount of head bobbing.")]
public float bobbingAmount = 0.2f;
[Tooltip("Midpoint of head bobbing on the y-axis.")]
public float midpoint = 2f;
[Header("Field of View Settings")]
[Tooltip("Normal field of view.")]
public float baseFOV = 60f;
[Tooltip("Field of view when sprinting.")]
public float sprintFOV = 70f;
[Tooltip("Speed of FOV change.")]
public float fovChangeSpeed = 5f;
[Tooltip("Flag to determine if the player is sprinting.")]
public bool isSprinting = false;
[Header("Camera Sway Settings")]
[Tooltip("Amount of camera sway.")]
public float swayAmount = 0.05f;
[Tooltip("Speed of camera sway.")]
public float swaySpeed = 4f;
// Timer for head bobbing calculations.
private float timer = 0.0f;
/// <summary>
/// Initialization method.
/// </summary>
void Start()
{
if (playerCamera == null)
{
Debug.LogError("PlayerCamera is not assigned in the inspector.");
return;
}
Cursor.lockState = CursorLockMode.Locked;
playerCamera.fieldOfView = baseFOV;
}
/// <summary>
/// Update is called once per frame.
/// </summary>
void Update()
{
HandleMouseLook();
HandleHeadBobbing();
HandleFieldOfView();
HandleCameraSway();
}
/// <summary>
/// Handles the mouse look functionality.
/// </summary>
void HandleMouseLook()
{
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
/// <summary>
/// Handles the head bobbing effect.
/// </summary>
void HandleHeadBobbing()
{
float waveslice = 0.0f;
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0)
{
timer = 0.0f;
}
else
{
waveslice = Mathf.Sin(timer);
timer = timer + bobbingSpeed;
if (timer > Mathf.PI * 2)
{
timer = timer - (Mathf.PI * 2);
}
}
Vector3 localPos = transform.localPosition;
if (waveslice != 0)
{
float translateChange = waveslice * bobbingAmount;
float totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
totalAxes = Mathf.Clamp(totalAxes, 0.0f, 1.0f);
translateChange = totalAxes * translateChange;
localPos.y = midpoint + translateChange;
}
else
{
localPos.y = midpoint;
}
transform.localPosition = localPos;
}
/// <summary>
/// Handles the dynamic field of view.
/// </summary>
void HandleFieldOfView()
{
if (isSprinting)
{
playerCamera.fieldOfView = Mathf.Lerp(playerCamera.fieldOfView, sprintFOV, fovChangeSpeed * Time.deltaTime);
}
else
{
playerCamera.fieldOfView = Mathf.Lerp(playerCamera.fieldOfView, baseFOV, fovChangeSpeed * Time.deltaTime);
}
}
/// <summary>
/// Handles the camera sway effect.
/// </summary>
void HandleCameraSway()
{
float movementX = Mathf.Clamp(Input.GetAxis("Mouse X") * swayAmount, -swayAmount, swayAmount);
float movementY = Mathf.Clamp(Input.GetAxis("Mouse Y") * swayAmount, -swayAmount, swayAmount);
Vector3 finalPosition = new Vector3(movementX, movementY, 0);
transform.localPosition = Vector3.Lerp(transform.localPosition, transform.localPosition + finalPosition, swaySpeed * Time.deltaTime);
}
}