Unity through camera movement to achieve continuous scrolling background __unity

Source: Internet
Author: User

The first technical blog, please take care of everyone.

Recently from the Web transformation to do unity development, the original contact with unity is mainly because it applied to a I am more interested in Open source project--mono, commonly known as "open source dotnet", is that you can use C # to do cross-platform application development. I was also at this time last year to learn about the use of C # to develop the Android project, and then have time to the relevant technical blog to fill it. In fact, unity in the Cross-platform hair is also a large extent by virtue of the mono, although the script is only part of the unity development.

To be honest, we still say unity. Learning Unity also has a period of time, always feel where all learned so point, but the higher-order technical point is not so familiar with. So the decision to regularly put their own learning summed up, and you share, we progress together, let us make progress together.

Learn the Luffy of the word "Unity Master, I will be." ”

The first blog is going to talk about how to achieve a moving effect similar to the background of a horizontal version of a cool game. Because an element like the background, if you make it into every hurdle, it's not quite practical to use a full map. We can use some of the same style of the same picture of the irregular switch to achieve our background effect, and when our protagonist moved to a position, the background of his path is no longer necessary to show, we can remove the previous map.

The general principle: we use the rays emitted from the edge to determine whether the current camera in this frame and the previous frame was photographed is not the same post, if not the point where the map, the camera has moved past, you can delete it.

First we need to set the Camera to orthographic orthogonal projection

When the scene is just loaded, we instantiate the 3-time background first, and position it separately.

Then, in the continuous movement of the Camera, the idea of the Camera center point and Edge is used to determine whether the last map is deleted. Bingo,so Easy

 void Update () {transform.
        Translate (New Vector3 (movespeed * time.deltatime, 0, 0));
        Raycasthit hit;
            if (Physics.raycast (Transform.position, Vector3.forward, out hit)) {float x = hit.point.x;

            Raycasthit Otherhit;
            Vector3 v1 = new Vector3 (x-10, TRANSFORM.POSITION.Y, transform.position.z);
            Vector3 v2 = v1 + vector3.forward * 20;
            Debug.drawline (v1, v2, color.red); if (Physics.raycast new Vector3 (x-10, TRANSFORM.POSITION.Y, transform.position.z), Vector3.forward, out
                Otherhit) {if (Lastplane.getinstanceid ()!=otherhit.collider.gameobject.getinstanceid ())
                        {if (lastplane!=null) {
                        Destroy (Lastplane);
                        Gameobject planeClone1 = (gameobject) instantiate (Plane.gameobject); planeclone1.trAnsform.position = new Vector3 (transform.position.x +15, 0, 0);
                } Lastplane = OtherHit.collider.gameObject; }
            }
        }
    }


The code is actually very simple, some of the constants in the code are also adjusted according to the size of the picture.

Take a look at the simple effect: Chopper



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.