Esempio n. 1
0
    void Start()
    {
        player = gameObject.transform.parent.GetComponentInChildren<Player>();

        //let's create the player
        items = new Items();
        characters = new Characters();
        if (items.LoadItems() && characters.LoadCharacters())
        {
            prefab = Resources.Load(characters.GetCharacter("Guy").GetDirectory(), typeof(GameObject));
            PlayerObject = Instantiate(prefab) as GameObject;
            PlayerObject.transform.position = new Vector3(0, 0.5f, 0);
            PlayerObject.transform.SetParent(gameObject.transform.parent.FindChild("PlayerObject").transform);

            for (int i = -4; i <= 4; i++) //initial 8 tiles
            {
                int rand = Random.Range(0, 2);
                item = items.GetItem(TilesArray[rand]);
                prefab = Resources.Load(item.GetDirectory(), typeof(GameObject));
                ObjectI = Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject;
                ObjectI.transform.position = new Vector3(i * 9, 0, 0) + item.GetBasePosition();
                ObjectI.transform.SetParent(gameObject.transform.FindChild("Tiles").transform);
                tiles.Add(ObjectI);
            }
        }
    }
Esempio n. 2
0
    // Update is called once per frame
    void Update()
    {
        lastRoad = tiles[tiles.Count - 1];
        distance = lastRoad.transform.position - GetPlayer().position;
        if(distance.x <= 30)
        {
            int rand = Random.Range(0, 2);
            item = items.GetItem(TilesArray[rand]);
            prefab = Resources.Load(item.GetDirectory(), typeof(GameObject));
            ObjectI = Instantiate(prefab) as GameObject;
            ObjectI.transform.position = new Vector3(lastRoad.transform.position.x + 9, 0, item.GetBasePosition().z);
            ObjectI.transform.SetParent(gameObject.transform.FindChild("Tiles").transform);
            tiles.Add(ObjectI);

            //let's add obstacles to this tile
            player = gameObject.transform.parent.GetComponentInChildren<Player>();
            if (player != null && player.started == true)
            {
                int obstaclesCount = Random.Range(1, 2); //1 to 2
                int[] separations = { -3, 0, 3 };
                for (int k = 1; k <= obstaclesCount; k++)
                {
                    string obstacleName = ObstaclesArray[Random.Range(0, ObstaclesArray.Length)];
                    item = items.GetItem(obstacleName);
                    basePosition = ObjectI.transform.position;
                    obstaclePosition = new Vector3(basePosition.x, 0, 0) + item.GetBasePosition() + new Vector3(separations[(int)Random.Range(0, 3)], 0, separations[(int)Random.Range(0, 3)]);
                    prefab = Resources.Load(item.GetDirectory(), typeof(GameObject));
                    ObstacleObject = Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject;
                    ObstacleObject.transform.position = obstaclePosition;
                    ObstacleObject.transform.SetParent(gameObject.transform.FindChild("Obstacles").transform);
                    obstacles.Add(ObstacleObject);
                }
            }
        }

        //let's delete roads outside of camera
           foreach(GameObject it in tiles)
        {
            distance = GetPlayer().position - it.transform.position;
            if(distance.x >= 20 && GetPlayer().position.x > it.transform.position.x && tiles.Count > 9)
            {
                Destroy(it);
                tiles.Remove(it);
                break;
            }
        }

        foreach(GameObject it in obstacles)
        {
            distance = GetPlayer().position - it.transform.position;
            if (Mathf.Abs(distance.x) >= 30 && GetPlayer().position.x > it.transform.position.x)
            {
                Destroy(it);
                obstacles.Remove(it);
                break;
            }
        }
    }