-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCameraScript
More file actions
35 lines (29 loc) · 1.15 KB
/
Copy pathCameraScript
File metadata and controls
35 lines (29 loc) · 1.15 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
# Camera-Follow
Camera Follow Script for a 2D Game.
// C# Code Starts from HERE....
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target;
public float smoothSpeed = 0.125f;
public Vector3 offset;
public Vector3 minValues, maxValues;
public void FixedUpdate()
{
Follow();
}
void Follow()
{
Vector3 desiredPosition = target.position + offset;
//Verify if the target position is out of bound or not.
//Limit it to the Min and Max values.
Vector3 boundPosition = new Vector3(Mathf.Clamp(desiredPosition.x, minValues.x, maxValues.x),
Mathf.Clamp(desiredPosition.y, minValues.y, maxValues.y),
Mathf.Clamp(desiredPosition.z, minValues.z, maxValues.z));
//Here in the above code, we have limited the x,y and z axes wrp to the desiredPosition(Player's pos)
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
transform.position = smoothedPosition;
}
}