Real Space Rush mobile game #update 3 - Adding and removing level in real time

This weekend I added functionality that will add new corridor when player activate the trigger and remove old corridor that is not in use anymore. In theory in game there will be only 2 corridors in one time. To illustrate it check this screenshot.


Currently I build only 1 type of corridor but I am planning to create at least 10 different. They will be stored in array and randomly pulled and added to scene. Corridors will not have the same sizes so I will have to make sure that transition between them is very smooth.

To main camera that is following character I added script (LevelCount) that will hold information how many levels are in the game.

public GameObject[] gameLevels; 
    public int levelsAdded = 0;
        
    // Use this for initialization
    void Start () {
        // Store gameLevels in list, so we can remove them later for performance
        gameLevels = new GameObject[2];
    }
    
    // Update is called once per frame
    void Update () {
    
    }


To my first corridor that is build in the game I added this script(LevelAppear).

public class LevelAppear : MonoBehaviour {
    
    private GameObject spawnPoint;
    private GameObject Corridor;
    bool PlayerEnteredZone;
    public GameObject Player;
    
    //hold information on levels
    private LevelCount LevelCountScript;
    
    // Use this for initialization
    void Start () {
        spawnPoint = GameObject.Find("spawnPoint");
        Corridor = GameObject.FindGameObjectWithTag("Corridor");
        LevelCountScript = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<LevelCount>();
        
    }
    
    // Update is called once per frame
    void Update () {
        if(PlayerEnteredZone)
        {
            AddLevel();
        }
    }
    
    void OnTriggerEnter(Collider player)
    {
            PlayerEnteredZone = true;
        
    }
    
    void AddLevel()
    {
        GameObject objectInstantiated = Instantiate(Corridor, new Vector3(transform.position.x + 28f, transform.position.y + 31f, transform.position.z + 20f), Quaternion.identity) as GameObject;
                
        if(LevelCountScript.levelsAdded == 2)
        {        
                LevelCountScript.levelsAdded = LevelCountScript.levelsAdded -2;
                Destroy(LevelCountScript.gameLevels[LevelCountScript.levelsAdded]);
                
        }
        
        if(LevelCountScript.levelsAdded == 1)
        {        
                LevelCountScript.levelsAdded = LevelCountScript.levelsAdded--;
                Destroy(LevelCountScript.gameLevels[LevelCountScript.levelsAdded]);
                
        }
        
        LevelCountScript.gameLevels[LevelCountScript.levelsAdded] = objectInstantiated;
        
        LevelCountScript.levelsAdded++;
        
        PlayerEnteredZone = false;
        
    }
    
}

It works fine however I am not very happy with hard coded position when adding new level (1st line of AddLevel function). I must find a better way of finding a point where new corridor will be added without having to hard code position. If anyone have any ideas I will be grateful.

Till next time.


Comments