Hello, everyone. Welcome to the Unity3D Game Development Series articles that I bring to you. My blog address is http://blog.csdn.net/qinyuanpei.
In 2D games such as daily parkour, because the game needs to show the feeling of motion, it usually keeps the game background rolling continuously to enhance the visual effect. So today, the blogger will lead everyone to achieve continuous rolling background!
First, let's talk about the principle and prepare two consecutive images (the blogger uses an image here, okay, I am lazy). We use an orthogonal projection camera to align with the first background, then, use the script to move the image from the right to the left. When the first image is removed from the camera's field of view, the second image is immediately followed by the first image and continues to move to the left, when the second image is removed from the camera's field of view, the first image is immediately followed by the second image, so that the background is continuously rolled back. Now, let's get started. First, we create an empty project. We need to add a Plane and name it Bg0 and a parallel light source. The parameter settings are as follows:
Next, we will drag and drop the prepared background image to Bg0, and then adjust the camera's field of view so that the first image is in its field of view, then we copy Bg0 and name it Bg1, adjust its position, and place it behind the first image. The effect of the scenario is as follows:
Now, let's write the script. The code is very simple:
using UnityEngine;using System.Collections;public class BGControll : MonoBehaviour {//Move Speed;private float mSpeed=3.0F;void Start () {}void Update () { //Translate form right to left transform.Translate(Vector3.right * Time.deltaTime * mSpeed); // If first background is out of camera view,then show sencond background if(transform.position.x<=-11.8F) { //We can chenge this value to reduce the wdith between 2 background transform.position=new Vector3(11.8F,transform.position.y,transform.position.z); }}}
I don't think you have any questions about the code. The only question may come from-11.8. This value is the critical value for the first image to exit the camera's field of view. How can this value be obtained, you can change the game view to a standard view, and drag the first background image with the mouse to get it. I will not talk about it here. Let's look at the running effect of the game.
Animation demos will be used for future effect demonstrations. Thank you!