-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCameraFollow2D.cs
57 lines (49 loc) · 1.6 KB
/
CameraFollow2D.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
using UnityEngine;
using System.Collections;
public class CameraFollow2D : MonoBehaviour
{
public float damping = 1.5f; //тест
public Vector2 offset = new Vector2(2f, 1f);
public static bool faceLeft;
private Transform player;
private int lastX;
private void Awake()
{
}
void Start()
{
offset = new Vector2(Mathf.Abs(offset.x), offset.y);
FindPlayer(faceLeft);
}
public void FindPlayer(bool playerFaceLeft)
{
player = GameObject.FindGameObjectWithTag("Player").transform;
lastX = Mathf.RoundToInt(player.position.x);
if (playerFaceLeft)
{
transform.position = new Vector3(player.position.x - offset.x, player.position.y + offset.y, transform.position.z);
}
else
{
transform.position = new Vector3(player.position.x + offset.x, player.position.y + offset.y, transform.position.z);
}
}
void Update()
{
if (player)
{
int currentX = Mathf.RoundToInt(player.position.x);
Vector3 target;
if (faceLeft)
{
target = new Vector3(player.position.x - offset.x, player.position.y + offset.y, transform.position.z);
}
else
{
target = new Vector3(player.position.x + offset.x, player.position.y + offset.y, transform.position.z);
}
Vector3 currentPosition = Vector3.Lerp(transform.position, target, damping * Time.deltaTime);
transform.position = currentPosition;
}
}
}