- Home /
How could I make a border around my grid world? (PIC INCLUDED)
I'm trying to make a border that would surround my grid world to kind of make it look like its on a tabletop. Heres my script for creating the world, and then you can see how i'm trying to create the border around it.
Can I set variables equal to vector 3's like this?
using UnityEngine;
using System.Collections;
public class CubeGrid : MonoBehaviour {
public Transform border;
public Transform grassLand;
public Transform dirtRoad;
public Transform city;
public Transform wolf;
public Transform bandit;
public int randNum = 0;
public int xCubes;
public int zCubes;
private Vector3 zBorder = new Vector3(0,1,zCubes);
private Vector3 xBorder = new Vector3(xCubes,1,0);
public bool fighting = false;
public bool enterWolf = false;
void Start () {
for ( int z = 0; z < zCubes; z++ )
{
for ( int x = 0; x < xCubes; x++ )
{
float rnd = Random.value;
if ( rnd < 0.05 )
{
Instantiate(city, new Vector3(x, 0, z), Quaternion.identity);
}
else if ( rnd < 0.15 )
{
Instantiate(dirtRoad, new Vector3(x, 0, z), Quaternion.identity);
}
else
{
Instantiate(grassLand, new Vector3(x, 0, z), Quaternion.identity);
randNum = Random.Range(0,50);
if(randNum <= 3){
Instantiate(wolf,new Vector3(x,0,z), Quaternion.identity);
}
else if(randNum ==4){
Instantiate(bandit, new Vector3(x,0,z), Quaternion.identity);
}
}
}
}
Instantiate(border, new Vector3(xBorder,0,zBorder), Quaternion.identity);
}
}
The yellow border is where I would want the border to be.
Answer by nesis · Feb 27, 2014 at 02:56 AM
Since you know the position, length, and width of the grid world, you can create the borders outside of that main for loop. Kudos for wanting to compact your code down for efficiency's sake, but at this level of complexity, you'd be ok to make a second for loop just for generating the border, in order to make your code easier to understand and, importantly, make it more modular so chunks of code can be added / removed easily as you keep developing this game.
In the code below, I've made the assumption your border is made up of objects that are 1x1 in size horizontally (eg editor cubes). I've also thrown in a few things I found handy when previously working on some procedural dungeon generation, hopefully they'll help too.
using UnityEngine;
using System.Collections;
public class CubeGrid : MonoBehaviour {
//these are now GameObjects so you can use Prefabs to define what they are
public GameObject border;
public GameObject grassLand;
public GameObject dirtRoad;
public GameObject city;
public GameObject wolf;
public GameObject bandit;
public int randNum = 0;
public int xCubes;
public int zCubes;
//no need for these, you can generate them on the fly
//private Vector3 zBorder = new Vector3(0,1,zCubes);
//private Vector3 xBorder = new Vector3(xCubes,1,0);
public bool fighting = false;
public bool enterWolf = false;
void Start () {
GenerateLandscapeAndObjects(xCubes,zCubes);
GenerateBorder(xCubes,zCubes);
}
private void GenerateLandscapeAndObjects(int xMax, int zMax) {
//create a GameObject to contain the landscape and objects
//to keep things neat in the Hierarchy tab if you decide
//to start generating huge landscapes later on
GameObject landscape = new GameObject("Landscape and Objects");
for ( int z = 0; z < zMax; z++ )
{
for ( int x = 0; x < xMax; x++ )
{
float rnd = Random.value;
if ( rnd < 0.05 )
{
DoInstantiate(city, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
}
else if ( rnd < 0.15 )
{
DoInstantiate(dirtRoad, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
}
else
{
DoInstantiate(grassLand, new Vector3(x, 0, z), Quaternion.identity,landscape.transform);
randNum = Random.Range(0,50);
if(randNum <= 3){
DoInstantiate(wolf,new Vector3(x,0,z), Quaternion.identity,landscape.transform);
}
else if(randNum ==4){
DoInstantiate(bandit, new Vector3(x,0,z), Quaternion.identity,landscape.transform);
}
}
}
}
}
private void GenerateBorder(int xMax, int zMax) {
//create a GameObject to contain the border
//to keep things neat in the Hierarchy tab if you decide
//to start generating huge landscapes later on
GameObject borderGameObject = new GameObject("Landscape and Objects");
//create border blocks along x axes
for ( int x = 0; x < xMax; x++ )
{
DoInstantiate(border, new Vector3(x,0,0), Quaternion.identity,borderGameObject.transform);
DoInstantiate(border, new Vector3(x,0,zMax), Quaternion.identity,borderGameObject.transform);
}
//create border blocks along z axes
for ( int z = 0; z < zMax; z++ )
{
DoInstantiate(border, new Vector3(0,0,z), Quaternion.identity,borderGameObject.transform);
DoInstantiate(border, new Vector3(xMax,0,z), Quaternion.identity,borderGameObject.transform);
}
}
//A helper function to let you organise GameObjects easily in
//the hierarchy by setting the transform.parent of the instantiated prefab.
private void DoInstantiate(GameObject prefab, Vector3 position, Quaternion rotation, Transform parent) {
Transform temp = ((GameObject)Instantiate(prefab,position,rotation)).transform;
temp.parent = parent;
}
}
Wow this just blew my $$anonymous$$d. Thank you very much for this. Is this actually functional script or is it just an example? For every instance of instantiate it says cannot convert .transform to .gameobject But I thought by adding .transform to the end of landscape/borderGameObject it would fix this but that doesn't seem to be the case.
I didn't notice your Instantiated() objects are Transforms, rather than GameObjects. This must mean you have at least 1 instance of each of them sitting in your scene, and assigned to your script through the Inspector tab?
If you use GameObject ins$$anonymous$$d of Transform as their type, you don't need an instance of them sitting in the scene. Click + drag each GameObject you want to Instantiate() from the Hierarchy tab and into the Project tab. This will turn them into a Prefab, which is a kind of GameObject that you can use with Instantiate(). Once you have all the Instantiate()-ed GameObjects saved as prefabs, you can click + drag them from the Project tab into the Inspector tab, and assign them to your script as before.
I've modified the script to change Transform to GameObject where needed.
@nesis: That's not related to the type. You can use Transform as the type for prefabs, as long as they have a Transform component (which they do since it's mandatory).
After a few variable changes an adjusting the size of my border here is the final result if you're interested :) This borders only placeholder of course. Thank you for all your help!
Your answer
Follow this Question
Related Questions
How to carry over variable (transform.position) and use it as Vector3 for instanitating a prefab 2 Answers
Help with creating a projectile based on Fixed Vector movement 1 Answer
Need help instantiating enemies between random locations 1 Answer
one var that is local in multiple objects still used by other scripts? 1 Answer
setting the text of an instantiated prefab's child's guitext object 1 Answer