-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraZoomZone.cs
100 lines (91 loc) · 3.98 KB
/
CameraZoomZone.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Easings;
public class CameraZoomZone : MonoBehaviour {
public bool reverse;
public CameraControl control;
public float minZoom;
public float maxZoom;
public CircleCollider2D circle;
public Vector3 minOffset;
public Vector3 maxOffset;
public bool ignoreDistanceEffects;
public Coroutine zoomOutCoroutine;
public float zoomOutTime = 3f;
public void ForceRecalculate(Collider2D other) {
if (other == null)
return;
if (ignoreDistanceEffects) {
control.offset = Vector3.Lerp(control.offset, maxOffset, 0.01f);
control.maxSize = Mathf.Lerp(control.maxSize, maxZoom, 0.01f);
} else {
float distance = Vector2.Distance(other.ClosestPoint(transform.position), transform.position);
float t = circle.radius - distance;
if (reverse) {
t = distance - circle.radius;
}
float zoom = (float)PennerDoubleAnimation.Linear(t, minZoom, maxZoom - minZoom, circle.radius);
float offsetX = (float)PennerDoubleAnimation.Linear(t, minOffset.x, maxOffset.x - minOffset.x, circle.radius);
float offsetY = (float)PennerDoubleAnimation.Linear(t, minOffset.y, maxOffset.y - minOffset.y, circle.radius);
float offsetZ = (float)PennerDoubleAnimation.Linear(t, minOffset.z, maxOffset.z - minOffset.z, circle.radius);
if (reverse) {
control.maxSize = Mathf.Min(zoom, minZoom);
offsetX = Mathf.Min(offsetX, minOffset.x);
offsetY = Mathf.Min(offsetY, minOffset.y);
offsetZ = Mathf.Min(offsetY, minOffset.z);
} else {
control.maxSize = Mathf.Max(zoom, minZoom);
offsetX = Mathf.Max(offsetX, minOffset.x);
offsetY = Mathf.Max(offsetY, minOffset.y);
offsetZ = Mathf.Max(offsetY, minOffset.z);
}
Vector3 offset = new Vector3(offsetX, offsetY, offsetZ);
control.offset = offset;
}
}
public void OnTriggerStay2D(Collider2D other) {
if (InputController.forbiddenTags.Contains(other.tag))
return;
if (other.gameObject == GameManager.Instance.playerObject) {
if (zoomOutCoroutine != null) {
StopCoroutine(zoomOutCoroutine);
zoomOutCoroutine = null;
}
ForceRecalculate(other);
}
}
public void OnTriggerEnter2D(Collider2D other) {
if (InputController.forbiddenTags.Contains(other.tag))
return;
if (other.gameObject == GameManager.Instance.playerObject) {
if (zoomOutCoroutine != null) {
StopCoroutine(zoomOutCoroutine);
zoomOutCoroutine = null;
}
ForceRecalculate(other);
}
}
public void OnTriggerExit2D(Collider2D other) {
if (InputController.forbiddenTags.Contains(other.tag))
return;
if (ignoreDistanceEffects) {
zoomOutCoroutine = StartCoroutine(ZoomOut());
}
}
IEnumerator ZoomOut() {
float timer = 0;
yield return null;
while (timer < zoomOutTime) {
timer += Time.deltaTime;
float zoom = (float)PennerDoubleAnimation.Linear(timer, control.maxSize, minZoom - control.maxSize, zoomOutTime);
float offsetX = (float)PennerDoubleAnimation.Linear(timer, control.offset.x, minOffset.x - control.offset.x, zoomOutTime);
float offsetY = (float)PennerDoubleAnimation.Linear(timer, control.offset.y, minOffset.y - control.offset.y, zoomOutTime);
float offsetZ = (float)PennerDoubleAnimation.Linear(timer, control.offset.z, minOffset.z - control.offset.z, zoomOutTime);
control.maxSize = zoom;
control.offset = new Vector3(offsetX, offsetY, offsetZ);
yield return null;
}
yield return null;
}
}