Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MapGenerator : MonoBehaviour
  6. {
  7.     public GameObject grassPrefab;
  8.     public GameObject dirtPrefab;
  9.     public Transform groundFolder;
  10.  
  11.     private List<int> heights = new List<int>();
  12.  
  13.     void Start()
  14.     {
  15.         for (int i = 0; i < 30; ++i)
  16.         {
  17.             GameObject grassTile = Instantiate(grassPrefab, groundFolder);
  18.             GameObject dirtTile = Instantiate(dirtPrefab, groundFolder);
  19.  
  20.             int groundHeight = 0;
  21.             if (i != 0)
  22.             {
  23.                 if(heights[i-1] == 0)
  24.                 {
  25.                     groundHeight = Random.Range(0, 2);
  26.  
  27.                     heights.Add(groundHeight);
  28.                 } else if (heights[i - 1] == 1)
  29.                 {
  30.                     groundHeight = Random.Range(0, 2);
  31.  
  32.                     heights.Add(groundHeight);
  33.                 }
  34.             }
  35.             else
  36.             {
  37.                 groundHeight = Random.Range(0, 1);
  38.  
  39.                 heights.Add(groundHeight);
  40.             }
  41.             if (groundHeight == 0)
  42.             {
  43.                 grassTile.transform.localPosition = new Vector3(-12.725f + i * grassTile.GetComponent<SpriteRenderer>().bounds.size.x, 0, 0);
  44.             }
  45.             else if(groundHeight == 1)
  46.             {
  47.                 dirtTile.transform.localPosition = new Vector3(-12.725f + i * grassTile.GetComponent<SpriteRenderer>().bounds.size.x, 0, 0);
  48.                 grassTile.transform.localPosition = new Vector3(-12.725f + i * grassTile.GetComponent<SpriteRenderer>().bounds.size.x, grassTile.GetComponent<SpriteRenderer>().bounds.size.y, 0);
  49.             }
  50.         }
  51.     }
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement