// Start is called before the first frame update void Start() { instance = this; Vector3 spawnPosition = startPoint.position; int tilesWithNoObstaclesTmp = tilesWithoutObstacles; for (int i = 0; i < tilesToPreSpawn; i++) { spawnPosition -= tilePrefab.startPoint.localPosition; Platform spawnedTile = Instantiate(tilePrefab, spawnPosition, Quaternion.identity) as Platform; if (tilesWithNoObstaclesTmp > 0) { spawnedTile.DeactivateAllObstacles(); tilesWithNoObstaclesTmp--; } else { spawnedTile.ActivateRandomObstacle(); } spawnPosition = spawnedTile.endPoint.position; spawnedTile.transform.SetParent(transform); spawnedTiles.Add(spawnedTile); } }
// Update is called once per frame void Update() { // Move the object upward in world space x unit/second. //Increase speed the higher score we get if (!gameOver && gameStarted) { transform.Translate(-spawnedTiles[0].transform.forward * Time.deltaTime * (movingSpeed + (score / 500)), Space.World); score += Time.deltaTime * movingSpeed; } if (mainCamera.WorldToViewportPoint(spawnedTiles[0].endPoint.position).z < 0) { //Move the tile to the front if it's behind the Camera Platform tileTmp = spawnedTiles[0]; spawnedTiles.RemoveAt(0); tileTmp.transform.position = spawnedTiles[spawnedTiles.Count - 1].endPoint.position - tileTmp.startPoint.localPosition; tileTmp.ActivateRandomObstacle(); spawnedTiles.Add(tileTmp); } if (gameOver || !gameStarted) { if (Input.GetKeyDown(KeyCode.Space)) { if (gameOver) { //Restart current scene Scene scene = SceneManager.GetActiveScene(); SceneManager.LoadScene(scene.name); } else { //Start the game gameStarted = true; } } } }