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

[SOLVED] How do I Make a Never-ending Sidescroller?

Discussion in 'Scripting' started by Blue Bean Apps, Aug 22, 2014.

  1. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    I've been working on a Flappy Bird-like game in which there is just one level which scrolls indefinitely. I've been having a hard time making this smooth transition, so any suggestions would be highly appreciated! Here is my code I'm using to add more to the level:
    Code (JavaScript):
    1. function OnCollisionEnter2D (col : Collision2D)
    2. {
    3.         if (col.gameObject.CompareTag ("Finish"))
    4.         {
    5.                 Debug.Log ("Load the next portion of the level, baby!!");
    6.                 Application.LoadLevelAdditive ("Level-2");
    7.         }
    8. }
    Using the above code, I check to see if the gameObject has collided with an object tagged "Finish." If there is a collision with tag "Finish," load the level additively. I have another script that spawns these objects randomly using Random.Range, so if the above code were to work correctly, each time the level loads, the layout of the random objects will be different, but the above code is not working. I want it to keep me moving through the level, but instead it restarts the level at the beginning. I'm using the free version, 4.3.4f1. I know about Application.LoadLevelAdditiveAsync, but that's only for Pro, so I can't use that. Is there anyway to do what I'm trying to do without going Pro?

    Thanks in advance!

    ~~ Chris
     
  2. ardizzle

    ardizzle

    Joined:
    Mar 28, 2013
    Posts:
    86
    Why load a new level? How I would do this is you guy is running across the screen and then you hit the collider you have set up now. That calls a function called next level that tells the game to either start spawning in a different pattern or add more objects or make like tile sets that scroll across the screen and when you advance a level change the tile sets. But that would be getting a bit more complicated.
     
  3. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    I think you should instantiate/object pool parts of the map, instead of loading a new one.
    And just spawn the objects back in. Front of the character when it went off screen. :0
     
  4. Asdlkjqweo

    Asdlkjqweo

    Joined:
    Jul 14, 2013
    Posts:
    20
    You can achieve that without loading another scene or level by using Instantiate method. If your player collide with the specified collider you've made, the collider will spawn the needed GameObjects on the scene from the ObjectPool. The GameObjects must be spawned ahead from the player direction. Then move the specified collider again ahead.
     
  5. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    154
    I shall answer this in 3 parts:

    good answer:
    for flappy bird you need only a little object pool, a few obstacles, 2 background layers and a player
    the trick is to never move the player or youll get problems with your translation eventually, move the obstacles and scroll the texture tiling for the background layers at n*distance;

    now the troll answer:
    never stop making it

    and the advice:
    be original, different, |N^EnT|vE; why make the nth clone of a S***ty game in the first place? being creative isn't hard and it also isn't science; building games is rocketSurgery.. perfect for mad scientists.. be mad, be different.. and enjoy it.

    pick of those whichever you like.
     
    Magiichan likes this.
  6. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    Wow! Thanks for the quick answers, everyone!
    Yeah, that does sound a but more complex. I'm new to UnityScript, so that is a little beyond me at the moment.
    I tried to Instantiate, but I guess I kept getting the for loop wrong or something because no matter how I set up the loop, Unity would crash. It's a good thing I'm addicted to saving! Could either of you possibly give me an example of Instantiate inside a for loop, preferably in UnityScript? I would be fine with C# as well, but seeing as how I'm more fluent in UnityScript, I would prefer UnityScript.
    My game is nothing like Flappy Bird. I mentioned Flappy Bird just to give an example of how I want my game to sidescroll. And if I never stopped making it, my game would never be complete, so that would be sort of pointless... Also, I see more problems happening when moving the background layers instead of the player, but that's just me.

    -- Chris
     
  7. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    what do you want to use a for-loop for?
    Code (JavaScript):
    1. var i;
    2. for (i = 0; i < myArray.length; i++) {
    3.     Instantiate(myArray[i], pos, Quaternion.identity);
    4. }
     
    Last edited: Aug 22, 2014
  8. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    I want to use a for loop to give the Instantiate some parameters telling it when to quit, because without it being in a loop, it will instantiate object after object forever until the game crashes. Therefore, I need to tell it how long to instantiate the object, which is why I would need a for loop. Thanks for the example! I'll give it a try.

    -- Chris
     
  9. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    @Blue Bean Apps
    Use the Update function, it gets called every frame, so just add a simple timer using Time.time. So it instantiates objects without making the main thread freeze.
     
  10. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    Could you give an example of instantiating using Time.time because I'm still learning UnityScript, so I don't know much when it comes to timing things right? I've tried using it any way I can think of, but I don't think I'm doing it right. Thanks! : )

    -- Chris
     
  11. Asdlkjqweo

    Asdlkjqweo

    Joined:
    Jul 14, 2013
    Posts:
    20
    You may use Coroutines when you are filling the scenes during runtime.

    Code (CSharp):
    1.  
    2. void Update()
    3. {
    4.      if(IsSpawningAllowed)
    5.      {
    6.           StartCoroutine(SpawnObjects(Number of Objects you want to spawn));
    7.      }
    8. }
    9.  
    10. IEnumerator SpawnObjects(int count)
    11. {
    12.     for(int i = 0; i < count; i++)
    13.     {
    14.           // Spawn object through instantiation
    15.           // or
    16.           // through Object Pool
    17.     }
    18.  
    19.      yield return null;             // I love null;
    20. }
    21.  
    I usually do that to prevent my Game or my Editor to freeze.
     
    Blue Bean Apps likes this.
  12. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    Alright, since I'm using UnityScript, I know I have to change void to function, but for the line "yield return null;" could I use null in UnistyScript or is that just a C# thing?

    -- Chris
     
  13. Graph

    Graph

    Joined:
    Jun 8, 2014
    Posts:
    154
    you just yield without return in js afaik
    yield [whatever]

    btw you did ask how to do a scrolling game like flappy bird.. the way i described is how it was done.
     
  14. Magiichan

    Magiichan

    Joined:
    Jan 5, 2014
    Posts:
    403
    this code spawns a prefab every 5 seconds.
    u could also use a coroutine as stated above.
    Code (JavaScript):
    1. #pragma strict
    2. var interval : float = 5f;
    3. var prefab : GameObject;
    4.  
    5. private var lastSpawnTime : float;
    6.  
    7. function Update() {
    8.      if(Time.time - lastSpawnTime > interval) {
    9.           Instantiate(prefab, position, Quaternion.identity);
    10.           lastSpawnTime = Time.time;
    11.      }
    12. }
     
    Blue Bean Apps likes this.
  15. Blue Bean Apps

    Blue Bean Apps

    Joined:
    Aug 10, 2014
    Posts:
    261
    I prefer not to move the background objects rather than the player because I've already made all my game based on the player moving, not the background, which would mean I would have to change a lot of things to reverse things. I also assumed that there was another way to do this.
    Thanks! This one finally worked! I appreciate you taking the time to dumb things down for me. : )

    -- Chris
     
    Magiichan likes this.