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