-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraToggle.cs
More file actions
68 lines (55 loc) · 1.91 KB
/
Copy pathCameraToggle.cs
File metadata and controls
68 lines (55 loc) · 1.91 KB
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
using UnityEngine;
public class CameraControl : MonoBehaviour
{
private float panSpeed = 40f;
private float zoomSpeed = 10f;
private float rotationSpeed = 120f;
[SerializeField] private float maxY = 150f;
[SerializeField] private float minY = 10f;
private Vector3 dragOrigin;
void Update()
{
PanCamera();
ZoomCamera();
RotateCamera();
}
void PanCamera()
{
float moveX = Input.GetAxis("Horizontal") * panSpeed * Time.deltaTime;
float moveZ = Input.GetAxis("Vertical") * panSpeed * Time.deltaTime;
// Calculate forward and right movement based on the camera's orientation
Vector3 move = transform.right * moveX + transform.forward * moveZ;
if (Input.GetKey(KeyCode.LeftShift) && transform.position.y >= minY)
{
move.y -= panSpeed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.Space) && transform.position.y <= maxY)
{
move.y += panSpeed * Time.deltaTime;
}
else
{
move.y = 0; // Prevent the camera from moving up/down
}
transform.Translate(move, Space.World);
}
void ZoomCamera()
{
float scroll = Input.GetAxis("Mouse ScrollWheel");
Vector3 direction = transform.forward * scroll * zoomSpeed;
transform.Translate(direction, Space.World);
}
void RotateCamera()
{
if (Input.GetMouseButtonDown(0)) // Left mouse button
{
dragOrigin = Input.mousePosition;
}
if (Input.GetMouseButton(0))
{
Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
transform.RotateAround(transform.position, transform.right, pos.y * rotationSpeed * Time.deltaTime);
transform.RotateAround(transform.position, Vector3.up, -pos.x * rotationSpeed * Time.deltaTime);
}
}
}