Search Unity

2D platformer Tiling Booleans that stop the gameObject's instantiation

Discussion in '2D' started by Fox-Handler, Jun 2, 2014.

  1. Fox-Handler

    Fox-Handler

    Joined:
    May 2, 2014
    Posts:
    74
    Hi This is a script for tililng in a 2D platformer. Basically whenever the camera gets to a certain point a gameObject will instantiate so that the player moves in a seamlessly never ending world. The only part that I'm not really able to understand is the last if and else statements.

    Code (CSharp):
    1.  if (rightOrLeft > 0)
    2. {
    3. newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
    4. }
    5. else
    6. {
    7. newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
    8. }
    I am confused by how the booleans are actually stopping the gameObject from being instantiated on the left or right side. I guess I am having a little trouble making the connection as code seems somewhat abstract.




    Code (CSharp):
    1.     using UnityEngine;
    2.     using System.Collections;
    3.  
    4.     [RequireComponent (typeof(SpriteRenderer))]
    5.  
    6.     public class Tiling : MonoBehaviour {
    7.  
    8.     public int offsetX = 2;
    9.  
    10.     public bool hasARightBuddy = false;
    11.     public bool hasALeftBuddy = false;
    12.  
    13.     public bool reverseScale = false;
    14.  
    15.  
    16.     private float spriteWidth = 0f;
    17.  
    18.     private Camera cam;
    19.     private Transform myTransform;
    20.  
    21.     void Awake () {
    22.     cam = Camera.main;
    23.     myTransform = transform;
    24.     }
    25.  
    26.     // Use this for initialization
    27.     void Start () {
    28.     SpriteRenderer sRenderer = GetComponent<SpriteRenderer>();
    29.     spriteWidth = sRenderer.sprite.bounds.size.x;
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update ()
    34.     {
    35.  
    36.  
    37.     if (hasALeftBuddy == false || hasARightBuddy == false)
    38.     {
    39.  
    40.     float camHorizontalExtend = cam.orthographicSize * Screen.width/Screen.height;
    41.  
    42.  
    43.     float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth/2) - camHorizontalExtend; //sprite width/2..51.2
    44.     //(0 + 51.2 - 26.67315 = 24.52685)
    45.     //(102.4 + 51.2 - 26.67315 = 126.92685) etc.. //clone of the myTransform
    46.     float edgeVisiblePositionLeft = (myTransform.position.x - spriteWidth/2) + camHorizontalExtend; // (0 - 51.2 + 26.67315 = -24.52685)
    47.     // (-102.4 - 51.2 + 26.67315 = -126.92685) etc..//clone of the myTransform
    48.  
    49.     if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)
    50.     {
    51.     MakeNewBuddy (1);
    52.     hasARightBuddy = true;
    53.     }
    54.     else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && hasALeftBuddy == false)
    55.     {
    56.     MakeNewBuddy (-1);
    57.     hasALeftBuddy = true;
    58.     }
    59.     }
    60.     }
    61.  
    62.  
    63.     void MakeNewBuddy (int rightOrLeft)
    64.     {
    65.  
    66.     Vector3 newPosition = new Vector3 (myTransform.position.x + spriteWidth * rightOrLeft, myTransform.position.y, myTransform.position.z);
    67.  
    68.     Transform newBuddy = Instantiate (myTransform, newPosition, myTransform.rotation) as Transform;
    69.  
    70.  
    71.     if (reverseScale == true)
    72.     {
    73.     newBuddy.localScale = new Vector3 (newBuddy.localScale.x*-1, newBuddy.localScale.y, newBuddy.localScale.z);
    74.     }
    75.  
    76.  
    77.     if (rightOrLeft > 0)
    78.     {
    79.     newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
    80.     }
    81.     else
    82.     {
    83.     newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
    84.     }
    85.     }
    86.     }
    87.     }
     
    Last edited: Jun 2, 2014