Unity3d 2D Game Background compensation effect

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.