forked from cmilr/Unity2D-Components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackgroundParallax.cs
executable file
·43 lines (34 loc) · 1.75 KB
/
BackgroundParallax.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
using UnityEngine;
using UnityEngine.Assertions;
public class BackgroundParallax : BaseBehaviour
{
public Transform[] backgrounds; // Array of all the backgrounds to be parallaxed.
public float parallaxScale; // The proportion of the camera's movement to move the backgrounds by.
public float parallaxReductionFactor; // How much less each successive layer should parallax.
public float smoothing; // How smooth the parallax effect should be.
private Transform cam; // Shorter reference to the main camera's transform.
private Vector3 previousCamPos; // The postion of the camera in the previous frame.
void Start()
{
cam = Camera.main.transform;
Assert.IsNotNull(cam);
previousCamPos = cam.position;
}
void Update()
{
// The parallax is the opposite of the camera movement since the previous frame, multiplied by the scale.
float parallax = (previousCamPos.x - cam.position.x) * parallaxScale;
// For each successive background...
for (int i = 0; i < backgrounds.Length; i++)
{
// ... set a target x position which is their current position plus the parallax multiplied by the reduction.
float backgroundTargetPosX = backgrounds[i].position.x + parallax * (i * parallaxReductionFactor + 1);
// Create a target position which is the background's current position but with it's target x position.
var backgroundTargetPos = new Vector3(backgroundTargetPosX, backgrounds[i].position.y, backgrounds[i].position.z);
// Lerp the background's position between itself and it's target position.
backgrounds[i].position = Vector3.Lerp(backgrounds[i].position, backgroundTargetPos, smoothing * Time.deltaTime);
}
// Set the previousCamPos to the camera's position at the end of this frame.
previousCamPos = cam.position;
}
}