daily pastebin goal
26%
SHARE
TWEET

Untitled

a guest Dec 15th, 2018 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TileMenager : MonoBehaviour {
  6.    
  7.     //Array for tile prefabs
  8.     public GameObject[] tilePrefabs;
  9.  
  10.     //Take players transform
  11.     private Transform playerTransform;
  12.     //Point to spawn first elememt
  13.     private float spawnZ = 32f;
  14.     //Lenght of tiled object
  15.     private float tileLength = 64f;
  16.     //Ammount of spawned elements on screen
  17.     private int ammTilesOnScreen = 7;
  18.  
  19.     // Use this for initialization
  20.     void Start () {
  21.         playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
  22.  
  23.         for (int i = 0; i < ammTilesOnScreen; i++)
  24.         {
  25.             SpawnTile();
  26.         }
  27.     }
  28.    
  29.     // Update is called once per frame
  30.     void Update () {
  31.         if (playerTransform.position.z > (spawnZ-ammTilesOnScreen*tileLength))
  32.         {
  33.             SpawnTile();
  34.         }
  35.     }
  36.  
  37.     private void SpawnTile(int prefabIndex = -1)
  38.     {
  39.         GameObject go;
  40.         go = Instantiate(tilePrefabs[0]) as GameObject;
  41.         go.transform.SetParent(transform);
  42.         //Vector3.forward = (0,0,1)
  43.         go.transform.position = Vector3.forward * spawnZ;
  44.         spawnZ += tileLength;
  45.     }
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top