A game about forced loneliness, made by TACStudios
1#if ENABLE_INPUT_SYSTEM && ENABLE_INPUT_SYSTEM_PACKAGE
2#define USE_INPUT_SYSTEM
3using UnityEngine.InputSystem;
4#endif
5
6using System.Collections.Generic;
7using UnityEngine;
8
9namespace UnityEngine.Rendering
10{
11 /// <summary>
12 /// Utility Free Camera component.
13 /// </summary>
14 [CoreRPHelpURLAttribute("Free-Camera")]
15 public class FreeCamera : MonoBehaviour
16 {
17 const float k_MouseSensitivityMultiplier = 0.01f;
18
19 /// <summary>
20 /// Rotation speed when using a controller.
21 /// </summary>
22 public float m_LookSpeedController = 120f;
23 /// <summary>
24 /// Rotation speed when using the mouse.
25 /// </summary>
26 public float m_LookSpeedMouse = 4.0f;
27 /// <summary>
28 /// Movement speed.
29 /// </summary>
30 public float m_MoveSpeed = 10.0f;
31 /// <summary>
32 /// Value added to the speed when incrementing.
33 /// </summary>
34 public float m_MoveSpeedIncrement = 2.5f;
35 /// <summary>
36 /// Scale factor of the turbo mode.
37 /// </summary>
38 public float m_Turbo = 10.0f;
39
40#if !USE_INPUT_SYSTEM
41 private static string kMouseX = "Mouse X";
42 private static string kMouseY = "Mouse Y";
43 private static string kRightStickX = "Controller Right Stick X";
44 private static string kRightStickY = "Controller Right Stick Y";
45 private static string kVertical = "Vertical";
46 private static string kHorizontal = "Horizontal";
47
48 private static string kYAxis = "YAxis";
49 private static string kSpeedAxis = "Speed Axis";
50#endif
51
52#if USE_INPUT_SYSTEM
53 InputAction lookAction;
54 InputAction moveAction;
55 InputAction speedAction;
56 InputAction yMoveAction;
57#endif
58
59 void OnEnable()
60 {
61 RegisterInputs();
62 }
63
64 void RegisterInputs()
65 {
66#if USE_INPUT_SYSTEM
67 var map = new InputActionMap("Free Camera");
68
69 lookAction = map.AddAction("look", binding: "<Mouse>/delta");
70 moveAction = map.AddAction("move", binding: "<Gamepad>/leftStick");
71 speedAction = map.AddAction("speed", binding: "<Gamepad>/dpad");
72 yMoveAction = map.AddAction("yMove");
73
74 lookAction.AddBinding("<Gamepad>/rightStick").WithProcessor("scaleVector2(x=15, y=15)");
75 moveAction.AddCompositeBinding("Dpad")
76 .With("Up", "<Keyboard>/w")
77 .With("Up", "<Keyboard>/upArrow")
78 .With("Down", "<Keyboard>/s")
79 .With("Down", "<Keyboard>/downArrow")
80 .With("Left", "<Keyboard>/a")
81 .With("Left", "<Keyboard>/leftArrow")
82 .With("Right", "<Keyboard>/d")
83 .With("Right", "<Keyboard>/rightArrow");
84 speedAction.AddCompositeBinding("Dpad")
85 .With("Up", "<Keyboard>/home")
86 .With("Down", "<Keyboard>/end");
87 yMoveAction.AddCompositeBinding("Dpad")
88 .With("Up", "<Keyboard>/pageUp")
89 .With("Down", "<Keyboard>/pageDown")
90 .With("Up", "<Keyboard>/e")
91 .With("Down", "<Keyboard>/q")
92 .With("Up", "<Gamepad>/rightshoulder")
93 .With("Down", "<Gamepad>/leftshoulder");
94
95 moveAction.Enable();
96 lookAction.Enable();
97 speedAction.Enable();
98 yMoveAction.Enable();
99#endif
100
101#if UNITY_EDITOR && !USE_INPUT_SYSTEM
102 List<InputManagerEntry> inputEntries = new List<InputManagerEntry>();
103
104 // Add new bindings
105 inputEntries.Add(new InputManagerEntry { name = kRightStickX, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Fourth, sensitivity = 1.0f, gravity = 1.0f, deadZone = 0.2f });
106 inputEntries.Add(new InputManagerEntry { name = kRightStickY, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Fifth, sensitivity = 1.0f, gravity = 1.0f, deadZone = 0.2f, invert = true });
107
108 inputEntries.Add(new InputManagerEntry { name = kYAxis, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "page up", altBtnPositive = "joystick button 5", btnNegative = "page down", altBtnNegative = "joystick button 4", gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f });
109 inputEntries.Add(new InputManagerEntry { name = kYAxis, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "q", btnNegative = "e", gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f });
110
111 inputEntries.Add(new InputManagerEntry { name = kSpeedAxis, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "home", btnNegative = "end", gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f });
112 inputEntries.Add(new InputManagerEntry { name = kSpeedAxis, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Seventh, gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f });
113
114 InputRegistering.RegisterInputs(inputEntries);
115#endif
116 }
117
118 float inputRotateAxisX, inputRotateAxisY;
119 float inputChangeSpeed;
120 float inputVertical, inputHorizontal, inputYAxis;
121 bool leftShiftBoost, leftShift, fire1;
122
123 void UpdateInputs()
124 {
125 inputRotateAxisX = 0.0f;
126 inputRotateAxisY = 0.0f;
127 leftShiftBoost = false;
128 fire1 = false;
129
130#if USE_INPUT_SYSTEM
131 var lookDelta = lookAction.ReadValue<Vector2>();
132 inputRotateAxisX = lookDelta.x * m_LookSpeedMouse * k_MouseSensitivityMultiplier;
133 inputRotateAxisY = lookDelta.y * m_LookSpeedMouse * k_MouseSensitivityMultiplier;
134
135 leftShift = Keyboard.current?.leftShiftKey?.isPressed ?? false;
136 fire1 = Mouse.current?.leftButton?.isPressed == true || Gamepad.current?.xButton?.isPressed == true;
137
138 inputChangeSpeed = speedAction.ReadValue<Vector2>().y;
139
140 var moveDelta = moveAction.ReadValue<Vector2>();
141 inputVertical = moveDelta.y;
142 inputHorizontal = moveDelta.x;
143 inputYAxis = yMoveAction.ReadValue<Vector2>().y;
144#else
145 if (Input.GetMouseButton(1))
146 {
147 leftShiftBoost = true;
148 inputRotateAxisX = Input.GetAxis(kMouseX) * m_LookSpeedMouse;
149 inputRotateAxisY = Input.GetAxis(kMouseY) * m_LookSpeedMouse;
150 }
151 inputRotateAxisX += (Input.GetAxis(kRightStickX) * m_LookSpeedController * k_MouseSensitivityMultiplier);
152 inputRotateAxisY += (Input.GetAxis(kRightStickY) * m_LookSpeedController * k_MouseSensitivityMultiplier);
153
154 leftShift = Input.GetKey(KeyCode.LeftShift);
155 fire1 = Input.GetAxis("Fire1") > 0.0f;
156
157 inputChangeSpeed = Input.GetAxis(kSpeedAxis);
158
159 inputVertical = Input.GetAxis(kVertical);
160 inputHorizontal = Input.GetAxis(kHorizontal);
161 inputYAxis = Input.GetAxis(kYAxis);
162#endif
163 }
164
165 void Update()
166 {
167 // If the debug menu is running, we don't want to conflict with its inputs.
168 if (DebugManager.instance.displayRuntimeUI)
169 return;
170
171 UpdateInputs();
172
173 if (inputChangeSpeed != 0.0f)
174 {
175 m_MoveSpeed += inputChangeSpeed * m_MoveSpeedIncrement;
176 if (m_MoveSpeed < m_MoveSpeedIncrement) m_MoveSpeed = m_MoveSpeedIncrement;
177 }
178
179 bool moved = inputRotateAxisX != 0.0f || inputRotateAxisY != 0.0f || inputVertical != 0.0f || inputHorizontal != 0.0f || inputYAxis != 0.0f;
180 if (moved)
181 {
182 float rotationX = transform.localEulerAngles.x;
183 float newRotationY = transform.localEulerAngles.y + inputRotateAxisX;
184
185 // Weird clamping code due to weird Euler angle mapping...
186 float newRotationX = (rotationX - inputRotateAxisY);
187 if (rotationX <= 90.0f && newRotationX >= 0.0f)
188 newRotationX = Mathf.Clamp(newRotationX, 0.0f, 90.0f);
189 if (rotationX >= 270.0f)
190 newRotationX = Mathf.Clamp(newRotationX, 270.0f, 360.0f);
191
192 transform.localRotation = Quaternion.Euler(newRotationX, newRotationY, transform.localEulerAngles.z);
193
194 float moveSpeed = Time.deltaTime * m_MoveSpeed;
195 if (fire1 || leftShiftBoost && leftShift)
196 moveSpeed *= m_Turbo;
197 transform.position += transform.forward * moveSpeed * inputVertical;
198 transform.position += transform.right * moveSpeed * inputHorizontal;
199 transform.position += Vector3.up * moveSpeed * inputYAxis;
200 }
201 }
202 }
203}