Skip to content

Commit 8168043

Browse files
authored
Camera keyboard control (#240)
using Q/E to control camera rotation, Z/X for zoom added momentum while stop controlling rotation/zoom, to have smooth animation (maybe? the relating speed can be changed in ESC Setting: Gameplay/Camera keyboard control
1 parent 7f7fdf4 commit 8168043

File tree

1 file changed

+48
-11
lines changed

1 file changed

+48
-11
lines changed

Assets/Scripts/Common/CameraRig.cs

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class CameraRig : BaseSingleton<CameraRig>
1212
[SerializeField] private int _rigLayer = default;
1313
[SerializeField] private float _defaultZoom = default;
1414
[SerializeField] private float _defaultAngle = default;
15-
[Setting("Gameplay/Camera", "Zoom Speed", "The speed when scrolling")]
15+
[Setting("Gameplay/Camera", "Mouse Scrolling Zoom Speed", "The speed when scrolling")]
1616
[SerializeField] private static float _zoomSpeed = 5;
1717
[SerializeField] private float _minZoom = 1;
1818
[SerializeField] private float _maxZoom = 400;
@@ -57,9 +57,15 @@ public static float Zoom
5757
private float _zoomRef;
5858
private Vector3 _posRef;
5959

60-
[Setting("Gameplay/Camera", "Room Keyboard Speed", "The speed when using WASD or arrow keys in room mode")]
61-
private static float _keyboardSpeed = 5;
62-
[Setting("Gameplay/Camera", "World Keyboard Speed", "The speed when using WASD or arrow keys in world mode")]
60+
[Setting("Gameplay/Camera keyboard control", "Room Keyboard Move Speed(WASD/ArrowKeys)", "The speed when using WASD or arrow keys in room mode (for position)")]
61+
private static float _keyboardSpeedMove = 5;
62+
[Setting("Gameplay/Camera keyboard control", "Room Keyboard Rotation Speed(Q/E)", "The speed when using Q/E in room mode (for rotation)")]
63+
private static float _keyboardSpeedRotation = 5;
64+
[Setting("Gameplay/Camera keyboard control", "Room Keyboard Rotation Speed(Z/X)", "The speed when using Z/X in room mode (for zoom)")]
65+
private static float _keyboardSpeedZoom = 5;
66+
[Setting("Gameplay/Camera keyboard control", "Room Keyboardspeed decay ratio", "Speed decay ratio after you stop keyboard controlling rotation/zoom")]
67+
private static float _keyboardSpeedmomentum = 0.95f;
68+
[Setting("Gameplay/Camera keyboard control", "World Keyboard Speed", "The speed when using WASD or arrow keys in world mode")]
6369
private static float _worldKeyboardSpeed = 5; // TODO: implement
6470

6571
private Vector3 _clickPos;
@@ -117,27 +123,58 @@ private void KeyboardControl()
117123
return;
118124

119125
KeyboardPosition();
126+
KeyboardZoom();
120127
KeyboardRotation();
121128
}
122129

130+
// save the current speen to calculate
131+
private static float _keyboardZoomPre = 1.0f;
132+
private static float _keyboardRotatePre = 1.0f;
133+
// control the rotation/zoom direction
134+
private static float _keyboardZoomDir = 1.0f;
135+
private static float _keyboardRotateDir = 1.0f;
136+
123137
private void KeyboardPosition()
124138
{
125139
var cameraRotation = Quaternion.Euler(0, Camera.main.transform.rotation.eulerAngles.y, 0);
126140
var input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
127141
var heightFactor = Mathf.Log10(-_boom.localPosition.z + 1);
128-
_targetPosition += cameraRotation * input * _keyboardSpeed * heightFactor * 10 * Time.deltaTime;
142+
_targetPosition += cameraRotation * input * _keyboardSpeedMove * heightFactor * 5 * Time.deltaTime;
143+
}
144+
145+
private void KeyboardZoom()
146+
{
147+
// if Z/X is pressed, use dir * speed, else use dir*momentum*speed to slow down smoothly
148+
if (Input.GetKey(KeyCode.Z)){
149+
_keyboardZoomPre = _keyboardSpeedZoom;
150+
_keyboardZoomDir = 1.0f;
151+
}
152+
else if(Input.GetKey(KeyCode.X)){
153+
_keyboardZoomPre = _keyboardSpeedZoom;
154+
_keyboardZoomDir = -1.0f;
155+
}
156+
else{
157+
_keyboardZoomPre = _keyboardZoomPre * _keyboardSpeedmomentum;
158+
}
159+
_targetZoom += 0.003f * _targetZoom / 2 * _keyboardZoomPre * _keyboardZoomDir;
160+
_targetZoom = Mathf.Clamp(_targetZoom, _minZoom, _maxZoom);
129161
}
130162

131163
private void KeyboardRotation()
132164
{
133-
if (Input.GetKey(KeyCode.Q))
134-
{
135-
_targetRotation.y += 25 * _keyboardSpeed * Time.deltaTime;
165+
// if Q/E is pressed, use dir * speed, else use dir*momentum*speed to slow down smoothly
166+
if (Input.GetKey(KeyCode.Q)){
167+
_keyboardRotatePre = _keyboardSpeedRotation;
168+
_keyboardRotateDir = 1.0f;
136169
}
137-
if (Input.GetKey(KeyCode.E))
138-
{
139-
_targetRotation.y -= 25 * _keyboardSpeed * Time.deltaTime;
170+
else if(Input.GetKey(KeyCode.E)){
171+
_keyboardRotatePre = _keyboardSpeedRotation;
172+
_keyboardRotateDir = -1.0f;
173+
}
174+
else{
175+
_keyboardRotatePre = _keyboardRotatePre * _keyboardSpeedmomentum;
140176
}
177+
_targetRotation.y += 5 * Time.deltaTime * _keyboardRotatePre * _keyboardRotateDir;
141178
}
142179

143180
private void MouseControl()

0 commit comments

Comments
 (0)