-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraFollow.cs
113 lines (101 loc) · 3.71 KB
/
CameraFollow.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour {
private Controller2D target;
public Vector2 focusAreaSize;
public Controller2D[] targetList;
public float verticalOffset;
public float lookAheadDstX;
public float lookSmoothTimeX;
public float verticalSmoothTime;
FocusArea focusArea;
float currentLookAheadX;
float targetLookAheadX;
float lookAheadDirX;
float smoothLookVelocityX;
float smoothVelocityY;
bool lookAheadStopped;
private void Start()
{
targetList = FindObjectsOfType<Controller2D>();
foreach (Controller2D potentialTarget in targetList)
{
if (potentialTarget.gameObject.tag == "Player")
target = potentialTarget;
}
focusArea = new FocusArea(target.collider.bounds, focusAreaSize);
}
private void LateUpdate()
{
focusArea.Update(target.collider.bounds);
Vector2 focusPosition = focusArea.center + Vector2.up * verticalOffset;
if(focusArea.velocity.x != 0)
{
lookAheadDirX = Mathf.Sign(focusArea.velocity.x);
if(Mathf.Sign(target.playerInput.x) == Mathf.Sign(focusArea.velocity.x) && target.playerInput.x != 0)
{
lookAheadStopped = false;
targetLookAheadX = lookAheadDirX * lookAheadDstX;
}
else
{
if(!lookAheadStopped)
{
targetLookAheadX = currentLookAheadX + (lookAheadDirX * lookAheadDstX - currentLookAheadX) / 4;
lookAheadStopped = true;
}
}
}
currentLookAheadX = Mathf.SmoothDamp(currentLookAheadX, targetLookAheadX, ref smoothLookVelocityX, lookSmoothTimeX);
focusPosition.y = Mathf.SmoothDamp(transform.position.y, focusPosition.y, ref smoothVelocityY, verticalSmoothTime);
focusPosition += Vector2.right * currentLookAheadX;
transform.position = (Vector3) focusPosition + Vector3.forward * -10;
}
private void OnDrawGizmos()
{
Gizmos.color = new Color(1, 0, 0, .5f);
Gizmos.DrawCube(focusArea.center, focusAreaSize);
}
struct FocusArea
{
public Vector2 center, velocity;
float left, right, top, bottom;
public FocusArea(Bounds targetBounds, Vector2 size)
{
left = targetBounds.center.x - size.x / 2;
right = targetBounds.center.x + size.x / 2;
bottom = targetBounds.min.y;
top = targetBounds.min.y + size.y;
velocity = Vector2.zero;
center = new Vector2((left + right) / 2, (top + bottom) / 2);
}
public void Update(Bounds targetBounds)
{
float shiftX = 0;
if(targetBounds.min.x < left)
{
shiftX = targetBounds.min.x - left;
}
else if(targetBounds.max.x > right)
{
shiftX = targetBounds.max.x - right;
}
left += shiftX;
right += shiftX;
float shiftY = 0;
if (targetBounds.min.y < bottom)
{
shiftY = targetBounds.min.y - bottom;
}
else if (targetBounds.max.y > top)
{
shiftY = targetBounds.max.y - top;
}
top += shiftY;
bottom += shiftY;
center = new Vector2((left + right) / 2, (top + bottom) / 2);
velocity = new Vector2(shiftX, shiftY);
}
}
}