Search Unity

Endless Scroll Background Loop

Discussion in '2D' started by johncc, Mar 5, 2015.

  1. johncc

    johncc

    Joined:
    Feb 23, 2015
    Posts:
    7
    Hi,

    I am attempting to create an endless background image loop that repeats endlessly. I have been successful at doing so by using the script below. However, the script does not work when applied to a group. Any help would be appreciated.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class SpriteRepeater : MonoBehaviour {
    4.     private Vector3 backPos;
    5.     public float width = 0f;
    6.     public float height = 0f;
    7.     private float X; private float Y;
    8.    
    9.     void OnBecameInvisible()
    10.     {
    11.         //calculate current position
    12.         backPos = gameObject.transform.position;
    13.         //calculate new position
    14.     //    print (backPos);
    15.         X = backPos.x + width*2;
    16.         Y = backPos.y + height*2;
    17.         //move to new position when invisible
    18.         gameObject.transform.position = new Vector3 (X, Y, 0f);
    19.     }
    20.    
    21. }
     
  2. NathanHold

    NathanHold

    Joined:
    Jul 17, 2013
    Posts:
    58
    I am not sure what you are really trying to do, however in the archives you can see there are some scrolling background videos that might help you:

    http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/2d-scrolling-backgrounds

    Now when you say groups what do you mean? That script you showed will need to be applied to a GameObject and it will only do that repositioning behaviour when OnBecameInvisible is called. You don't show us when OnBecameInvisible is called and we kind of need to know where that is called to see why it's not calling it for other GameObjects.
     
  3. johncc

    johncc

    Joined:
    Feb 23, 2015
    Posts:
    7
    When I say "Groups" I'm referring to the hierarchy panel. When I drop one sprite over another sprite in the hierarchy panel it creates a group (with a little arrow on the left). The reason I wanted to apply it to a group is because when I apply it to the game objects independently it only works with 2. It doesn't work when trying to rotate through 3 or more images.
     
  4. NathanHold

    NathanHold

    Joined:
    Jul 17, 2013
    Posts:
    58
    So in that case, if it's working how you say you should be able to drag the script onto the parent of the group with all of the 'scrolling backgrounds' beneath it. From there you have to make sure you set the 'width' member variable to the correct width (Most probably not 0!) and it should work.

    But we still don't know where your method OnBecameInvisible() is called from so it's pretty hard to know what exactly is happening. I think your logic on checking if the background is invisible is what is failing here if you want to do it with each bg having it's own script.