-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamFollow.cs
207 lines (174 loc) · 5.81 KB
/
CamFollow.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
using UnityEngine;
public class CamFollow : MonoBehaviour
{
public Transform target;
private Vector3 targetPosition;
private Vector3 desiredPosition;
private float x, y, z;
public bool followTargetX, followTargetY, followTargetZ;
[SerializeField] private float _cameraFollowSpeed;
public bool customOffset;
public Vector3 customOffsetPos;
private Vector3 _startOfffset;
[Header("Update")]
public bool update;
public bool fixedUpdate;
public bool lateUpdate;
[Header("Zoom")]
[SerializeField] private bool _zoomActive;
[SerializeField] private float _zoomMinimum;
[SerializeField] private float _zoomMaximum;
[SerializeField] private float _zoomSensitivity;
private Vector2 _mousePosition;
private float _zoom;
private bool rightClickHold;
private bool leftClickHold;
[Header("Drag Camera")]
[SerializeField] private bool _moveXActive;
[Range(0.0f, 3f)]
[SerializeField] private float moveXSensitivity;
[SerializeField] private bool _moveYActive;
[Range(0.0f, 3f)]
[SerializeField] private float moveYSensitivity;
private float viewPortWidth;
private float viewPortHeight;
private float ratioH;
private float ratioW;
[SerializeField] private float increment;
private float incrementY;
private float incrementX;
private Camera _mainCamera;
[Range(0.0f, 0.5f)]
[SerializeField] private float yBorderThickness;
[Range(0.0f, 0.5f)]
[SerializeField] private float xBorderThinckness;
private float aspectRatio;
private bool isPortrait;
private void Awake()
{
_startOfffset = transform.position - target.position;
_mainCamera = Camera.main;
viewPortWidth = _mainCamera.pixelRect.width;
viewPortHeight = _mainCamera.pixelRect.height;
}
private void Start()
{
targetPosition = new Vector3(target.position.x, target.position.y, target.position.z);
desiredPosition = targetPosition + (customOffset ? customOffsetPos : _startOfffset);
transform.position = desiredPosition;
}
void Update()
{
rightClickHold = (Input.GetMouseButton(1));
leftClickHold = (Input.GetMouseButton(0));
if (target != null)
{
_mousePosition = _mainCamera.ScreenToViewportPoint(Input.mousePosition);
targetPosition = new Vector3(target.position.x, target.position.y, target.position.z);
desiredPosition = targetPosition + (customOffset ? customOffsetPos : _startOfffset);
x = desiredPosition.x * .5f;
y = desiredPosition.y;
z = desiredPosition.z;
if (rightClickHold)
{
viewPortHeight = _mainCamera.pixelRect.height;
viewPortWidth = _mainCamera.pixelRect.width;
if (viewPortHeight > viewPortWidth)
{
aspectRatio = (viewPortHeight / viewPortWidth);
isPortrait = true;
}
else if (viewPortWidth > viewPortHeight)
{
aspectRatio = (viewPortWidth / viewPortHeight);
isPortrait = false;
}
ratioH = (viewPortHeight / viewPortWidth);
ratioW = (viewPortWidth / viewPortHeight);
incrementX = (increment * ((isPortrait) ? aspectRatio : 1) * moveXSensitivity);
incrementY = (increment * ((!isPortrait) ? aspectRatio : 1) * moveYSensitivity);
}
if (_zoomActive)
Zoom();
if (_moveXActive && rightClickHold)
MoveOffsetX();
if (_moveYActive && rightClickHold)
MoveOffestY();
if (update)
PositionLerp();
}
}
private void LateUpdate()
{
if (target != null && lateUpdate)
PositionLerp();
}
private void FixedUpdate()
{
if (target != null && fixedUpdate)
PositionLerp();
}
private void PositionLerp()
{
transform.position = Vector3.Lerp(transform.position,
new Vector3(followTargetX ? x : transform.position.x, followTargetY ? y : transform.position.y, followTargetZ ? z : transform.position.z), Time.deltaTime * _cameraFollowSpeed);
}
public void StopFollow()
{
followTargetX = false;
followTargetY = false;
followTargetZ = false;
}
private void Zoom()
{
_zoom = Input.GetAxis("Mouse ScrollWheel") * _zoomSensitivity;
if (_zoom < 0 && (customOffset ? customOffsetPos.y : _startOfffset.y) > _zoomMinimum)
{
if (customOffset)
customOffsetPos = new Vector3(customOffsetPos.x, ((customOffsetPos.y + _zoom) < 0) ? 0 : customOffsetPos.y + _zoom, customOffsetPos.z);
else
_startOfffset = new Vector3(_startOfffset.x, ((_startOfffset.y + _zoom) < 0) ? 0 : _startOfffset.y + _zoom, _startOfffset.z);
}
else if (_zoom > 0 && (customOffset ? customOffsetPos.y : _startOfffset.y) < _zoomMaximum)
{
if (customOffset)
customOffsetPos = new Vector3(customOffsetPos.x, ((customOffsetPos.y + _zoom) < 0) ? 0 : customOffsetPos.y + _zoom, customOffsetPos.z);
else
_startOfffset = new Vector3(_startOfffset.x, ((_startOfffset.y + _zoom) < 0) ? 0 : _startOfffset.y + _zoom, _startOfffset.z);
}
}
private void MoveOffestY()
{
if (_mousePosition.y < yBorderThickness)
{
if (customOffset)
customOffsetPos = new Vector3(customOffsetPos.x, customOffsetPos.y, customOffsetPos.z - incrementY);
else
_startOfffset = new Vector3(_startOfffset.x, _startOfffset.y, _startOfffset.z - incrementY);
}
else if (_mousePosition.y > (1 - yBorderThickness))
{
if (customOffset)
customOffsetPos = new Vector3(customOffsetPos.x, customOffsetPos.y, customOffsetPos.z + incrementY);
else
_startOfffset = new Vector3(_startOfffset.x, _startOfffset.y, _startOfffset.z + incrementY);
}
}
private void MoveOffsetX()
{
if (_mousePosition.x < xBorderThinckness)
{
if (customOffset)
customOffsetPos = new Vector3(customOffsetPos.x - incrementX, customOffsetPos.y, customOffsetPos.z);
else
_startOfffset = new Vector3(_startOfffset.x - incrementX, _startOfffset.y, _startOfffset.z);
}
else if (_mousePosition.x > (1 - xBorderThinckness))
{
if (customOffset)
customOffsetPos = new Vector3(customOffsetPos.x + incrementX, customOffsetPos.y, customOffsetPos.z);
else
_startOfffset = new Vector3(_startOfffset.x + incrementX, _startOfffset.y, _startOfffset.z);
}
}
}