Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.02 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class GameController : MonoBehaviour
  6. {
  7.     [SerializeField]
  8.     private int SpawnCount = 35;
  9.     [SerializeField]
  10.     private GameObject cactus;
  11.     [SerializeField]
  12.     private GameObject[] SpawneObjects;
  13.  
  14.  
  15.     void Awake() {
  16.  
  17.         SpawneObjects = new GameObject[SpawnCount];
  18.         cactus = Resources.Load("Cactus_01") as GameObject;      
  19.        
  20.     }
  21.  
  22.     void Start() {
  23.  
  24.         for (int i = 0; i < SpawnCount; i++) {
  25.  
  26.             SpawneObjects[i] = cactus;
  27.             SpawneObjects[i].transform.position = transform.position;
  28.  
  29.         }
  30.  
  31.         Vector3 NewPos = Vector3.zero;
  32.         int a = 0;
  33.         for (int i = 0; i < SpawnCount-1; i++) {
  34.  
  35.  
  36.         N:  NewPos.x = Random.Range(16.0f, 85.0f);
  37.         NewPos.y = 0.1f;
  38.         NewPos.z = Random.Range(17.0f, 86.0f);
  39.  
  40.         a = i;
  41.        
  42.             if(i == 0){
  43.                 SpawneObjects[i] = SpawnStaticObject(NewPos);
  44.                 continue;
  45.             }
  46.             if ( (Mathf.Abs(NewPos.x) - 10 > Mathf.Abs(SpawneObjects[i-1].transform.position.x) &&
  47.                 Mathf.Abs(NewPos.z) - 10 > Mathf.Abs(SpawneObjects[i - 1].transform.position.z)) ||
  48.  
  49.                 //(Mathf.Abs(NewPos.x) - 7 > Mathf.Abs(SpawneObjects[i].transform.position.x) &&
  50.                 // Mathf.Abs(NewPos.z) - 7 > Mathf.Abs(SpawneObjects[i].transform.position.z)) ||
  51.  
  52.                 (Mathf.Abs(NewPos.x) - 10 > Mathf.Abs(SpawneObjects[i+1].transform.position.x) &&
  53.                  Mathf.Abs(NewPos.z) - 10 > Mathf.Abs(SpawneObjects[i+1].transform.position.z)))  {
  54.  
  55.                     Debug.Log(i);  
  56.                 SpawneObjects[i] = SpawnStaticObject(NewPos);
  57.                 continue;
  58.  
  59.             } else  {
  60.                 i = a;
  61.                 goto N;
  62.             }
  63.  
  64.         }
  65.  
  66.        
  67.     }
  68.     private GameObject SpawnStaticObject(Vector3 pos) {
  69.  
  70.         return Instantiate(cactus, pos, Quaternion.identity) as GameObject;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement