-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraOrbitFollowZoom.cs
56 lines (48 loc) · 1.68 KB
/
CameraOrbitFollowZoom.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
using UnityEngine;
[AddComponentMenu("Camera-Control/Camera Orbit Follow Zoom")]
public class CameraOrbitFollowZoom : MonoBehaviour
{
[SerializeField] Transform target;
[SerializeField] float distance = 5.0f;
[SerializeField] float xSpeed = 10.0f;
[SerializeField] float ySpeed = 10.0f;
[SerializeField] float yMinLimit = -90f;
[SerializeField] float yMaxLimit = 90f;
[SerializeField] float distanceMin = 5f;
[SerializeField] float distanceMax = 500f;
[SerializeField] float smoothTime = 0.2f;
float x = 0.0f;
float y = 0.0f;
float xSmooth = 0.0f;
float ySmooth = 0.0f;
float xVelocity = 0.0f;
float yVelocity = 0.0f;
float distanceSmooth;
float distanceVelocity=0.0f;
void LateUpdate()
{
if (target)
{
GetInputs();
MoveCamera();
}
}
private void MoveCamera()
{
xSmooth = Mathf.SmoothDamp(xSmooth, x, ref xVelocity, smoothTime);
ySmooth = Mathf.SmoothDamp(ySmooth, y, ref yVelocity, smoothTime);
distanceSmooth = Mathf.SmoothDamp(distanceSmooth, distance,ref distanceVelocity, smoothTime);
transform.localRotation = Quaternion.Euler(ySmooth, xSmooth, 0);
transform.position = transform.rotation * new Vector3(0.0f, 0.0f, -distanceSmooth) + target.position;
}
private void GetInputs()
{
if (Input.GetMouseButton(1))
{
x += Input.GetAxis("Mouse X") * xSpeed;
y -= Input.GetAxis("Mouse Y") * ySpeed;
y = Mathf.Clamp(y, yMinLimit, yMaxLimit);
}
distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel") * distance, distanceMin, distanceMax);
}
}