Search Unity

TheWebExpert wants to know: How do I make a parallax scrolling background?

Discussion in '2D' started by TheWebExpert, Apr 24, 2015.

  1. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    198
    I've seen several tutorials on the web, yet have been unsuccessful in getting them to work. What I want is a foreground image, with two moving background images behind it. The sample I found at http://www.raywenderlich.com/78675/unity-new-gui-part-1.html is a great example of what I'm looking for.
    However, when I modify the code so that my foreground image stays still and the background images keep moving, the "mouse" character they've created moves forward as well. I want an animated character that DOESN'T move (or, one that I can move without affecting the movement of the background images).
    I've tried about a dozen tutorials so far, and even following the directions to the letter (or as closely as possible), my backgrounds don't move. (As far as "close as possible," I'm talking about their version of Unity being different from mine, for instance with the Legacy Shaders.
    What I'd like is a sample I can just "plug in," as it were, and/or a tutorial which is known to work. Can anybody help?

    I want to learn this... but following a tutorial that doesn't work teaches me nothing. With a piece of working code, I can study it & figure out the how & why. Thanks!
     
  2. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    198
    If you can at least point me to a WORKING tutorial, I'd be very grateful.
     
  3. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    I do not know of any tutorial but a simple way is just make two GameObjects in your scene. Name one Background and the other Foreground. Put your background image(s) under (children of) the Background GO. Put your foreground images under the Foreground GO. Now just move the Background GO and Foreground GO as you want.
     
  4. sleekdigital

    sleekdigital

    Joined:
    May 31, 2012
    Posts:
    133
    This might not be exactly what you are looking for, but you might learn something from it....
     
  5. TheWebExpert

    TheWebExpert

    Joined:
    Apr 14, 2015
    Posts:
    198
    Thank you very much for your reply. The answer was indeed helpful. I played with the code a bit and came up with this modification:

    Code (csharp):
    1.  
    2. void Update ()
    3. {
    4. //The parallax is the opposite of the camera movement,
    5. //because the previous frame because the previous
    6. //frame multiplied by the scale
    7. float parallax1 = (previousCamPos.x – cam.position.x) * parallaxScales[0];
    8. float parallax2 = (previousCamPos.x – cam.position.x) * parallaxScales[1];
    9. //Set a target x position which is the current position
    10. //plus the parallax
    11. float imageTargetPosX1 = images[0].position.x + parallax1;
    12. float imageTargetPosX2 = images[1].position.x + parallax2;
    13. //Create a target position which is the images’s
    14. //current position with its target x position
    15. Vector3 imageTargetPos1 = new Vector3 (imageTargetPosX1 – 2, images[0].position.y, images[0].position.z);
    16. //Fade between current position & target position using lerp
    17. images[0].position = Vector3.Lerp (images[0].position, imageTargetPos1, smoothing * Time.deltaTime);
    18. Vector3 imageTargetPos2 = new Vector3 (imageTargetPosX2 – 3, images[1].position.y, images[1].position.z);
    19. //Fade between current position & target position using lerp
    20. images[1].position = Vector3.Lerp (images[1].position, imageTargetPos2, smoothing * Time.deltaTime);
    21. //Set previous camera position to the camera’s position at the end of the frame
    22. previousCamPos = cam.position;
    23. }
    24.  
    What I have now is the still foreground (great) and two separate moving backgrounds (even better)! I’ll add variables later so that I can control the speed without having to edit the code, but that’s fine.
    The main problem I have now is this: a). How do I know when image #1 has moved far enough left so that I now have to make a repeat copy of it x pixels to the right so that the animation stays seamless? b). How to I actually make that copy?
    Both images are seamless (they’re also temporary, while I’m learning to do this). I know it can be done, but I just need that teensy eensy bit more help…
     
  6. LeftyTwoGuns

    LeftyTwoGuns

    Joined:
    Jan 3, 2013
    Posts:
    260
    There's a few very good parallax scrolling assets on the Asset Store. Ranging from free to still pretty cheap