The development of the main scene parallax effect of Unity3d game development experience

Source: Internet
Author: User


AnalysisWhat is parallax scrolling? Degree Niang explanation: lets the multi-layer background move at different speed, forms the three-dimensional movement effect. as can be seen, the home scene background is broadly divided into3layer, grassland, mountains and rivers and clouds, each layer of speed are different. then the analysis, although the degree Niang explanation is explained by the speed, but uses the speed to calculate is not suitable, because the main layer (namely grass layer) The rolling is follows our finger the movement, therefore should convert the speed to the displacement to calculate. Since the displacement is used to calculate the displacement of each layer, how can we synchronize the multiple layers, I use the normalization method, the whole scene of the scroll as a0~1the normalized displacement between each layer is simply multiplied by the maximum displacement of the respective layer. with the normalized displacement to realize the Parallax scrolling, the next step is to let the scene scroll following the finger movement, now actually very good implementation, just according to the distance of the finger movement (Xdirection) and the maximum displacement calculation of the main layer. The final analysis is inertia, after the finger left the screen after the scene will still scroll for a period of time, in fact, is a deceleration movement. (finally analyzed, people will not analyze, suppressed to now have internal injuries )
ImplementThe first step is to achieveSetPosition ()method, set the normalized position by this method, and then move all the layers to the correct location.
  1. Public transform[] layers;//each layer of Transform.
  2. Public float[] offsets;//corresponds to the maximum displacement of each layer.
  3. int count;
  4. Float location;//defines a normalized position.
  5. void Start ()
  6. {
  7. Count = Layers.length;
  8. }
  9. public void SetPosition (float position)
  10. {
  11. Location = MATHF.CLAMP01 (position);
  12. for (int i = 0; i < count; i++)
  13. {
  14. Layers[i].localposition = new Vector3 (offsets[i] * location, 0, 0);
  15. }
  16. }
Copy Code

whenLocationto be0The leftmost content of each layer is displayed,1displays the rightmost content of each layer. Assign the root node of each layer to theLayersproperty. As shown in the following:

The overall effect is as follows:

then move the layer to the left until the rightmost content is displayed, at which point the root nodeXThe coordinates are the maximum displacements we want, assigning this value to the correspondingOffsets(this value is a negative number). The second step is to realize the scrolling of the finger following.
  1. BOOL dragged;
  2. float Lasttouch;
  3. float Dragoffset;
  4. float Touchtopos;
  5. void Start ()
  6. {
  7. Count = Layers.length;

  8. dragged = false;
  9. Touchtopos = 1f/screen.width * camera.orthographicsize * 2f * CAMERA.ASPECT/MATHF.ABS (offsets[0]);
  10. }
  11. void Update ()
  12. {
  13. if (Input.getmousebuttondown (0))
  14. {
  15. dragged = true;
  16. Lasttouch = input.mouseposition.x;
  17. }
  18. if (input.getmousebuttonup (0))
  19. {
  20. dragged = false;
  21. }
  22. if (dragged)
  23. {
  24. float Currtouch = input.mouseposition.x;
  25. Dragoffset = Lasttouch-currtouch;
  26. Lasttouch = Currtouch;
  27. Dragoffset *= Touchtopos;
  28. Location + = Dragoffset;
  29. SetPosition (location);
  30. }
  31. }
Copy Code

This code is to calculate the change in the position of the scene when each finger is moved.--dragoffsetAnd adds that amount to the currentLocationuse after addingSetPositionto update each layer. The third step is to realize inertia. For more highlights, please click here ."Dog Planing Learning Net"
  1. BOOL tweened;
  2. float Tweentime;
  3. const float maxtweentime = 0.5f;
  4. void Start ()
  5. {
  6. Count = Layers.length;
  7. dragged = false;
  8. tweened = false;
  9. Touchtopos = 1f/screen.width * camera.orthographicsize * 2f * CAMERA.ASPECT/MATHF.ABS (offsets[0]);
  10. }
  11. void Update ()
  12. {
  13. if (Input.getmousebuttondown (0))
  14. {
  15. dragged = true;
  16. tweened = false;
  17. Lasttouch = input.mouseposition.x;
  18. }
  19. if (input.getmousebuttonup (0))
  20. {
  21. dragged = false;
  22. tweened = true;
  23. Tweentime = 0f;
  24. }
  25. if (dragged)
  26. {
  27. ......
  28. } else if (tweened)
  29. {
  30. Tweentime + = Time.deltatime;
  31. if (Tweentime > Maxtweentime)
  32. {
  33. tweened = false;
  34. } else
  35. {
  36. float offset = Dragoffset * (1-tweentime/maxtweentime);
  37. Location + = offset;
  38. SetPosition (location);
  39. }
  40. }
  41. }
Copy Code

This is used in the second step to calculate theDragoffset, the last calculation (that is, before the finger leaves the screen)Dragoffsetat maximum inertia timeMaxtweentimeinternal reduction to0and accumulate toLocationto achieve the effect of deceleration movement, and the distance of movement is related to the speed of the finger movement. finally finished, or the code is cool 

The development of the main scene parallax effect of Unity3d game development experience

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.