forked from nhathuy7996/ReUseScript_Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraMove.cs
49 lines (43 loc) · 1.6 KB
/
CameraMove.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMove : MonoBehaviour {
public float damping = 1.5f;
public Transform _target;
public Vector3 offset = new Vector3(2f, 1f);
private bool faceLeft;
private int lastX;
private float dynamicSpeed;
private Camera _cam;
void Start()
{
offset = new Vector2(Mathf.Abs(offset.x), offset.y);
FindPlayer();
_cam = gameObject.GetComponent<Camera>();
}
public void FindPlayer()
{
lastX = Mathf.RoundToInt(_target.position.x);
transform.position = new Vector3(_target.position.x + offset.x, _target.position.y + offset.y, _target.position.z + offset.z);
}
void FixedUpdate()
{
if (_target)
{
int currentX = Mathf.RoundToInt(_target.position.x);
if (currentX > lastX) faceLeft = false; else if (currentX < lastX) faceLeft = true;
lastX = Mathf.RoundToInt(_target.position.x);
Vector3 target;
if (faceLeft)
{
target = new Vector3(_target.position.x - offset.x, _target.position.y + offset.y+dynamicSpeed, _target.position.z + offset.z+dynamicSpeed);
}
else
{
target = new Vector3(_target.position.x + offset.x, _target.position.y + offset.y+dynamicSpeed, _target.position.z + offset.z+dynamicSpeed);
}
Vector3 currentPosition = Vector3.Lerp(transform.position, target, damping * Time.deltaTime);
transform.position = currentPosition;
}
}
}