Skip to content

Instantly share code, notes, and snippets.

@Domiii
Last active July 9, 2016 14:30
Show Gist options
  • Save Domiii/3cefc9d69c6d9e4b7f449db66bc92368 to your computer and use it in GitHub Desktop.
Save Domiii/3cefc9d69c6d9e4b7f449db66bc92368 to your computer and use it in GitHub Desktop.
[Unity v5.3] Create brick wall
/**
* Spawns a wall along the X and Y axes of the WallSpawner, using cubes of given size and materials
*/
using UnityEngine;
using System.Collections;
public class WallSpawner : MonoBehaviour {
public int Nx = 10;
public int Ny = 10;
public float CubeSize = 2;
public Material[] Materials = new Material[0];
private float Gap = 0.1f;
// Use this for initialization
void Start () {
CreateBricks ();
}
void Update () {
// things to try out: rotate the entire wall slowly
// transform.RotateAround(Vector3.up, 0.01f);
}
public void CreateBricks() {
for (var j = 0; j < Ny; ++j) {
var m = j % Materials.Length;
for (var i = 0; i < Nx; ++i) {
var cube = GameObject.CreatePrimitive (PrimitiveType.Cube);
cube.transform.SetParent(transform, false);
cube.transform.localScale = new Vector3(CubeSize, CubeSize, CubeSize);
var x = i * (CubeSize + Gap) + Gap;
var y = j * (CubeSize + Gap) + Gap;
cube.transform.localPosition = new Vector3 (x, y, 0);
cube.AddComponent<Rigidbody>();
cube.AddComponent<BoxCollider>();
cube.GetComponent<MeshRenderer> ().material = Materials[m];
m = (m + 1) % Materials.Length;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment