Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Moving backgrounds seperate from each other

Discussion in 'Scripting' started by lostudo, Oct 16, 2014.

  1. lostudo

    lostudo

    Joined:
    Jul 25, 2013
    Posts:
    14
    Hi,
    I have three backgrounds moving one after other in a loop till the end of the game. But whenever a spike happens or something causes a lag or fps drops in game, one of the backgrounds comes a little back and there becomes a gap between two of them. My script is like below and a sample pic is also below. How is it happening and how can i prevent it.
    Thanks for your help.

    Code (CSharp):
    1.  for (int i = 0; i < ground3.Length; i++)
    2.             {
    3.                 ground3[i].transform.position -= Vector3.right * ground3Speed * Time.deltaTime;
    4.                 if (ground3[i].transform.position.x <= -33.0f)
    5.                 {
    6.                     ground3[i].transform.position = new Vector3(64, ground3[i].transform.position.y, 0);
    7.                 }
    8.             }
     
    Last edited: Oct 16, 2014
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    instead of setting it to an exact position, can you offset it by a consistent amount each time?
    ie you set to 64, but it should be oldPosition + (size * count)
     
  3. lostudo

    lostudo

    Joined:
    Jul 25, 2013
    Posts:
    14
    Actually i was first doing it like that but the problem was happening than i tried to set it to a static position but same problem happens both way.