-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraMovement.cs
More file actions
90 lines (74 loc) · 2.96 KB
/
Copy pathCameraMovement.cs
File metadata and controls
90 lines (74 loc) · 2.96 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMovement : MonoBehaviour
{
public float verticalOffset = 0.15f;
public Transform target;
public float multi = 1.1f;
public float springBack = 2;
public float maxDistance = 5f;
public float resetSpeed = 2f;
private float maxDistanceInit;
public float initialScale = 1f;
public float scaleIncrease = 0.01f;
private float scale;
private Rigidbody2D rb;
private Vector3 newVelocity;
private float distancex;
private float distancey;
private float initialZPosition = -10f;
private float initialCameraSize;
// Start is called before the first frame update
void Start()
{
rb = target.GetComponent<Rigidbody2D>();
initialZPosition = transform.position.z;
initialCameraSize = GetComponent<Camera>().orthographicSize;
maxDistanceInit = maxDistance;
}
// Update is called once per frame
void FixedUpdate()
{
scale = initialScale + scaleIncrease * target.transform.childCount;
GetComponent<Camera>().orthographicSize = initialCameraSize * scale;
maxDistance = scale * maxDistanceInit;
Vector3 currentPosition = transform.position;
newVelocity = rb.velocity * Time.deltaTime;
distancex = currentPosition.x - rb.position.x;
distancey = currentPosition.y - rb.position.y;
if (Mathf.Abs(distancex) < maxDistance)
{
currentPosition.x += newVelocity.x * multi;
}
else
{
currentPosition.x += newVelocity.x / multi;
}
if (((distancex < 0) && (newVelocity.x > 0)) || ((distancex > 0) && (newVelocity.x < 0)))
{
currentPosition.x += newVelocity.x * springBack;
}
if (((distancey < 0) && (newVelocity.y > 0)) || ((distancey > 0) && (newVelocity.y < 0)))
{
currentPosition.y += newVelocity.y * springBack;
}
if (Mathf.Abs(currentPosition.y - rb.position.y) < maxDistance)
{
currentPosition.y += newVelocity.y * multi;
}
else
{
currentPosition.y += newVelocity.y;
}
currentPosition.z = initialZPosition / multi;
currentPosition.x = Mathf.Clamp(currentPosition.x, rb.position.x - maxDistance, rb.position.x + maxDistance);
currentPosition.y = Mathf.Clamp(currentPosition.y, rb.position.y - maxDistance, rb.position.y + maxDistance);
transform.position = currentPosition;
if (rb.velocity.magnitude < 0.1f)
{
Vector2 resetPosition = Vector2.Lerp(new Vector2(transform.position.x, transform.position.y), new Vector2(target.position.x, target.position.y), Time.deltaTime * resetSpeed);
transform.position = new Vector3(resetPosition.x, resetPosition.y, initialZPosition);
}
}
}