-
Notifications
You must be signed in to change notification settings - Fork 0
/
FollowCamera.cs
72 lines (55 loc) · 2.64 KB
/
FollowCamera.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowCamera : MonoBehaviour {
// Distance in the x axis the player can move before the camera follows.
public float xMargin = 1.5f;
// Distance in the y axis the player can move before the camera follows.
public float yMargin = 1.5f;
// How smoothly the camera catches up with its target movement in the x axis.
public float xSmooth = 1.5f;
// How smoothly the camera catches up with its target movement in the y axis.
public float ySmooth = 1.5f;
// The maximum x and y coordinates the camera can have.
public Vector2 maxXandY;
// The minimum x and y coordinates the camera can have.
public Vector2 minXandY;
// Reference to the player's transform.
public Transform player;
void Awake() {
// Setting up the reference.
player = GameObject.Find("Player").transform;
transform.position = new Vector3(player.transform.position.x, player.transform.position.y,transform.position.z);
if (player == null) {
Debug.LogError("Player object not found");
}
}
void FixedUpdate() {
// By default the target x and y coordinates of the camera are it’s current x and y coordinates.
float targetX = transform.position.x;
float targetY = transform.position.y;
// If the player has moved beyond the x margin…
if (CheckXMargin()) {
// the target x coordinate should be a Lerp between the camera’s current x position and the player’s current x position
targetX = Mathf.Lerp(transform.position.x, player.position.x, xSmooth * Time.fixedDeltaTime);
}
// If the player has moved beyond the y margin…
if (CheckYMargin()) {
// the target y coordinate should be a Lerp between the camera’s current y position and the player’s current y position
targetY = Mathf.Lerp(transform.position.y, player.position.y, ySmooth * Time.fixedDeltaTime);
}
// The target x and y coordinates should not be larger than the maximum or smaller than the minimum.
targetX = Mathf.Clamp(targetX, minXandY.x, maxXandY.x);
targetY = Mathf.Clamp(targetY, minXandY.y, maxXandY.y);
// Set the camera’s position to the target position with the same z component.
transform.position = new Vector3(targetX, targetY,transform.position.z);
}
bool CheckXMargin() {
// Returns true if the distance between the camera and the player in the x axis is greater than the x margin.
return Mathf.Abs(transform.position.x - player.position.x) > xMargin;
}
bool CheckYMargin() {
// Returns true if the distance between the camera and the player in the y axis is greater than the y margin.
return Mathf.Abs(transform.position.y - player.position.y) > yMargin;
}
}