Coding a scrolling background

We will keep going with the same game scene we left at the end of the previous recipe, so just stick with the instructions!

How to do it...

  1. In the Scripts folder of your Project panel create a new C# script and name it Back_Manager.
  2. Double-click on the script to open it in Monodevelop.
  3. Let's begin by creating the required variables: we need one public Transform variable to store the reference to the prefab to be used as the background panels and a few private vars to define things like the scale of the panels, the distance from the character, and a reference to the game character.

    From a game logic perspective, the most important variable we are adding to the script is an Array[] variable type that we use to manage the three panels that repeat endlessly in the background. To achieve that, add the following lines at the beginning of the script:

    public Transform backBrick;
    
    private Transform[] backArray=new Transform[3]; 
    private Transform thisChar;
    private float distance;
    private float farDistance;
    private Vector3 brickScale;
  4. In the Start() function we put the instructions to initialize the variables and create a service float variable to help us make longer lines of code more readable. These instructions could be added to an Awake() function instead of Start(), but to keep things easy, we prefer to use the Start() function.
  5. Add the following lines to the Start() function of the script:
    thisChar = GameObject.Find ("Constructor").GetComponent<Transform>();
    farDistance = 30.0f;
    brickScale=new Vector3(60,60,1);
    float xPosition;
  6. Now we add a for() cycle to instantiate the panels in the scene; add them to the array and set their scale. The following code goes into the Start() function too:
    for (int i=0; i<3; i++) {
        
      xPosition=(brickScale.x*i);
      backArray[i]=(Transform)Instantiate(backBrick,new Vector3(xPosition,0,farDistance),Quaternion.identity); 
      backArray[i].localScale=brickScale;
    }
  7. In the Update() function we plan to run a routine that keeps repeating and checks the horizontal distance between the character and the background panels. For this reason, instead of coding the routine into the Update() function itself, we make a custom function and then call that function in Update(). The custom function name is CheckDistance() and this is the code to be added to it:
    void CheckDistance(){
    
      for (int i=0; i<3; i++) {
        
        distance=thisChar.transform.position.xbackArray[i].transform.position.x;
    
        if(Mathf.Abs (distance) > (brickScale.x * 1.5) &&distance > 0){
          backArray[i].Translate( 3* brickScale.x,0,0); 
          break;
        }
    
        if(Mathf.Abs (distance) > (brickScale.x * 1.5) &&distance < 0){ 
          backArray[i].Translate(-3*brickScale.x,0,0);
          break;
        }
      }
    }
  8. Access the Update() function and add a call to CheckDistance() there using the following lines:
    void Update () {
      CheckDistance ();
    }

    In case you are missing something, the following screenshot shows the complete BackgroundManager script:

    How to do it...
  9. Go back to Unity, create an empty GameObject in the scene and name it back_manager. You can refer to the following screenshot:
    How to do it...
  10. Now drag Back_Manager onto the back_manager game object in the scene. The attached script, called Back Brick, should appear in Inspector as it's one public variable.
  11. Drag back_prefab from the Project panel onto the Back Brick slot of the BackgroundManager script in the Inspector, as shown in the following screenshot:
    How to do it...

How it works...

The scripts attached to back_manager in the scene take care of instantiating the panels with the right position and scale. As the character moves left or right, the scripts check their position and decide how to translate the background panels so one is always behind the player. It just needs us to manually create a reference to the prefab to be used for the background panels, which is what we did with the last step of the recipe.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.80.100