Wednesday, October 1, 2014

Unity Script: Building a Small Village with JS / UnityScript


This here's an introduction to instantiating onto multiple lots at the beginning of your scene (or script). We'll start with some good ol' fashioned JS. You'll recognize this structure as a stretched version of BuildHere which we used to instantiate just the one building on the one lot. This is the way to do it if you want to avoid an Array and For-Loop situation. Good for small stuff but not great for generating large towns.

Basically we're just multiplying everything by six.


The Script

#pragma strict

var building : GameObject;

var lot1 : GameObject;
var lot2 : GameObject;
var lot3 : GameObject;
var lot4 : GameObject;
var lot5 : GameObject;
var lot6 : GameObject;

var buildHere : GameObject;

var resourceLoadString1 : String;
var resourceLoadString2 : String;
var resourceLoadString3 : String;
var resourceLoadString4 : String;
var resourceLoadString5 : String;
var resourceLoadString6 : String;

var thisPos1 : Vector3 = new Vector3();
var thisPos2 : Vector3 = new Vector3();
var thisPos3 : Vector3 = new Vector3();
var thisPos4 : Vector3 = new Vector3();
var thisPos5 : Vector3 = new Vector3();
var thisPos6 : Vector3 = new Vector3();

var thisRot1 : Quaternion = new Quaternion();
var thisRot2 : Quaternion = new Quaternion();
var thisRot3 : Quaternion = new Quaternion();
var thisRot4 : Quaternion = new Quaternion();
var thisRot5 : Quaternion = new Quaternion();
var thisRot6 : Quaternion = new Quaternion();

function Start () {
//sets your building's position to the same coordinates as your lot
thisPos1 = lot1.transform.position;
thisPos2 = lot2.transform.position;
thisPos3 = lot3.transform.position;
thisPos4 = lot4.transform.position;
thisPos5 = lot5.transform.position;
thisPos6 = lot6.transform.position;
//this raises your building up a bit above the lot so you don't get z-fighting flickers
thisPos1[1] = thisPos1[1] + 0.45f;
thisPos2[1] = thisPos2[1] + 0.45f;
thisPos3[1] = thisPos3[1] + 0.45f;
thisPos4[1] = thisPos4[1] + 0.45f;
thisPos5[1] = thisPos5[1] + 0.45f;
thisPos6[1] = thisPos6[1] + 0.45f;

thisRot1 = lot1.transform.rotation;
thisRot2 = lot2.transform.rotation;
thisRot3 = lot3.transform.rotation;
thisRot4 = lot4.transform.rotation;
thisRot5 = lot5.transform.rotation;
thisRot6 = lot6.transform.rotation;

//this string should be your path through your Resources folder
resourceLoadString1 = "MoveConst/Construction Frame 1";
resourceLoadString2 = "MoveConst/Construction Frame 2";
resourceLoadString3 = "MoveConst/Construction Frame 3";
resourceLoadString4 = "MoveConst/Construction Frame 4";
resourceLoadString5 = "MoveConst/Construction Frame 5";
resourceLoadString6 = "MoveConst/Construction Frame 6";

buildHere = Instantiate(Resources.Load(resourceLoadString1), thisPos1, thisRot1) as GameObject;
buildHere = Instantiate(Resources.Load(resourceLoadString2), thisPos2, thisRot2) as GameObject;
buildHere = Instantiate(Resources.Load(resourceLoadString3), thisPos3, thisRot3) as GameObject;
buildHere = Instantiate(Resources.Load(resourceLoadString4), thisPos4, thisRot4) as GameObject;
buildHere = Instantiate(Resources.Load(resourceLoadString5), thisPos5, thisRot5) as GameObject;
buildHere = Instantiate(Resources.Load(resourceLoadString6), thisPos6, thisRot6) as GameObject;



  • Make sure to set your Resource.Load() paths correctly and 
  • Assign your Lots in the Inspector if you don't GameObject.Find() them.

All blog posts by UnityGirl are inspired by my work with Brunelleschi: Age of Architects on the Aesop Games team. Check out our Crowd Funding page(s)!

No comments:

Post a Comment