Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Best method of creating level for 2D game. Advice?

Discussion in '2D' started by Swindy, Jul 23, 2015.

  1. Swindy

    Swindy

    Joined:
    Jan 15, 2014
    Posts:
    4
    Hi guys,
    I'm creating a game similar to Adventure Time's Super Jumping Finn , if anyone of you have played that, and I'm just wondering what the best way to have the character move across the screen would be.

    My initial thought was to have the player launch across the screen with a stationary background, but that would mean having a large image loading at the start of the game, which is something I feel like I should be avoiding because I want to deploy this on mobile (I might be wrong about this though, if anyone wants to let me know thanks).

    That led me to know my second thought, in which I spawn in the first 500 units of background over multiple tiles, for example, then when the player reaches a certain tile, I would spawn in more. I hope you understand what I mean by that. However the issue I had with this method is that the player will be moving at high speeds and I'm wondering if Unity can keep up with the speed of spawning in and destroying the tiles.

    Which leads me to my last idea, having the player moving stationary in the x axis but having him move vertically, while having the background moving.

    I'm not entirely sure what the best method would be for this particular type of game, so I was wondering if anybody here could point me in the right direction? Thanks in advance
     
  2. itzclay36

    itzclay36

    Joined:
    Apr 11, 2015
    Posts:
    25
    I don't spawn or destroy my tiles, I move them.

    1st I create about 6 background sprites to cover my background. Put a collider in each of them, on their own layer that doesn't collide with the player. Then, I attach an object with a long collider to my camera, and set the collider to trigger and move it behind the camera.

    Set that object on a layer that only collides with the background tiles.

    Attach a script to the collider object, and inside it when it triggers on a background, take that background and move it forward in front of the other tiles. You can do this by taking the collider width on the background and multiplying it by the number of tiles you have.

    And that's basically it. You'll have an infinite scrolling background.

    Here's my code, not even sure if I wrote it myself honestly. I got the basic idea from a tutorial, but think I had to tweak it some.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BGLooper : MonoBehaviour {
    5.  
    6.     //move bg items when collission detected.
    7.     public int numBGPanels = 5;
    8.  
    9.  
    10.     void OnTriggerEnter2D(Collider2D collider) {
    11.         if (collider.tag == "BackgroundTile") {
    12.             float widthofBGObject = ((BoxCollider2D)collider).size.x - 0.02f;
    13.             Vector3 pos = collider.transform.position;
    14.             pos.x += widthofBGObject * numBGPanels;
    15.             collider.transform.position = pos;
    16.         }
    17.     }
    18.  
    19.  
    20.  
    21. }
    22.  
     
  3. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    If the image is basically the same & scrolls infinitely I've used the tile offset to scroll my image. Basically I use a plane as my background, put the image on it, tile it if necessary, then just scroll the offset. You can probably be tricky & use any movement on the main character to actually move the background image to create the illusion that the actual character is moving.
     
  4. itzclay36

    itzclay36

    Joined:
    Apr 11, 2015
    Posts:
    25
    Oh, I forgot to mention.

    Another thing I do with my background is I have it move forward as my character moves as well, just not as fast. This way the background isn't moving at the same pace as the foreground and looks like it's actually in the distance.

    I add this code to make the backgrounds move.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Skymover : MonoBehaviour {
    5.    Rigidbody2D player;
    6.  
    7.    void Start () {
    8.      GameObject player_go = GameObject.FindGameObjectWithTag("Player");
    9.      if (player_go == null) {
    10.        Debug.LogError("Couldn't find an object with tag 'Player'");
    11.        return;
    12.      }
    13.      player = player_go.GetComponent<Rigidbody2D>();
    14.    }
    15.  
    16.    void FixedUpdate() {
    17.      float vel = player.velocity.x * 0.95f;
    18.      transform.position = transform.position + Vector3.right * vel * Time.deltaTime;
    19.    }
    20.  
    21. }
    22.  
     
  5. Swindy

    Swindy

    Joined:
    Jan 15, 2014
    Posts:
    4
    Thanks guys, I think I've got an idea of how to do things.
    I might spawn in more than 6, depending on the results I see :)
     
  6. Swindy

    Swindy

    Joined:
    Jan 15, 2014
    Posts:
    4
    Just a question about this line of code, I don't understand the brackets (parenthesis what ever :p) surrounding the BoxCollider2D and Collider. Could you explain the syntax or whatever the technical term for why you have to do that? xD
     
  7. itzclay36

    itzclay36

    Joined:
    Apr 11, 2015
    Posts:
    25
    Originally I had written it as collider.GetComponent<BoxCollider2D>(), but then unity changed it if I remember correctly.

    So I'm honestly not sure.
     
  8. Swindy

    Swindy

    Joined:
    Jan 15, 2014
    Posts:
    4
    Ok, I was wondering if that's another way of writing it :D Thanks!
     
  9. BusyCat

    BusyCat

    Joined:
    Jan 9, 2015
    Posts:
    37
    I believe it's called casting. Well, I know it's called casting but I'm not 100% certain this is the C# way of casting. Said differently, the 'collider' variable is being cast (converted) to a BoxCollider2D type of collider within the parens and '.size' is a method being called on the casting result.