public class Backgroundparallax:monobehaviour
{
Public transform[] backgrounds; Background object
public float Parallaxscale; Zoom factor for camera move offset value
Public float parallaxreductionfactor;//background compensation coefficients for each layer
public float Smoothing; The amount of smooth movement of the background object
Private Transform cam;//Camera
Private Vector3 previouscampos;//The location of the camera's previous stitch
void Awake ()
{
Cam = Camera.main.transform;
}
void Start ()
{
Previouscampos = cam.position;
}
void Update ()
{
Get the offset value of the camera and the previous frame
Float parallax = (previouscampos.x-cam.position.x) * Parallaxscale;
for (int i = 0; i < backgrounds. Length; i++)
{
Each layer of the background compensation movement of the speed of different, I the greater the speed of compensation will be greater
Float BACKGROUNDTARGETPOSX = backgrounds[i].position.x + Parallax * (i * parallaxreductionfactor + 1);
Y axis not moving
Vector3 Backgroundtargetpos = new Vector3 (BACKGROUNDTARGETPOSX, BACKGROUNDS[I].POSITION.Y, backgrounds[i].position.z );
Smooth movement
Backgrounds[i].position = Vector3.lerp (backgrounds[i].position, Backgroundtargetpos, smoothing * Time.deltaTime);
}
Update the position of the previous frame camera previouscampos = cam.position; } }
Unity3d 2D Game Background compensation effect