How to make certain tiles a certain state
I am trying to make a grid system and I finally found a grid generator using tiles. But, my next step is to make a system that creates allows me to create boundaries between the different "counties". I thought maybe I could define each individual tile and then define the tile to wich county it belonged to (for starters neutral, red blue, green, yellow). Since I am a noob at C# I need help integrating this into what I believe is the correct place to put it in.
So far my code is as fallows
using UnityEngine;
using System.Collections;
public class mapGenerator : MonoBehaviour {
public Transform tilePrefab;
public Vector2 mapSize;
[Range(0,1)]
public float outlinePercent;
void Start() {
genMap ();
}
public void genMap(){
string holderName = "Generated map";
if (transform.FindChild (holderName)) {
DestroyImmediate (transform.FindChild (holderName).gameObject);
}
Transform mapHolder = new GameObject (holderName).transform;
mapHolder.parent = transform;
for (int x = 0; x < mapSize.x; x++) {
for (int y = 0; y < mapSize.y; y++) {
Vector3 tilePos = new Vector3 (-mapSize.x / 2 + 0.5f + x, 0, -mapSize.y/2 + 0.5f + y);
Transform newTile = Instantiate (tilePrefab, tilePos, Quaternion.Euler(Vector3.right*90)) as Transform;
newTile.localScale = Vector3.one * (1 - outlinePercent);
newTile.parent = mapHolder;
}
}
}
}
You should probably make a Tile script that you add to your tile prefab. $$anonymous$$ake tilePrefab
to be of type Tile ins$$anonymous$$d of Transform and whatever info you need each tile to have, add a variable for it in the Tile class.
What do you mean @Nose$$anonymous$$ills How would I do that exactly. Im quite noobish and the only thing that made sense and actually worked was making the tile script. And to clear this up. That script generates a 100x100 tile area and I would need to make a way to create the borders and also have them change throughout time.
Answer by NoseKills · Jun 14, 2017 at 04:21 PM
Create a tile class
public class Tile : MonoBehaviour {
public bool Walkable;
public bool IsCountyBorder;
public int SomeInt;
}
Drag that script onto your tile prefab. Then change the type of your prefab variable
public Transform tilePrefab;
to
public Tile tilePrefab;
Then drag the prefab to the tilePrefab field again in inspector. Now you can get a reference to the Tile script of the tile every time you instantiate one
Tile newTile = Instantiate (tilePrefab, tilePos, Quaternion.Euler(Vector3.right*90)) as Tile;
newTile.IsCountyBorder = true;
I would imagine you want to put the created tiles into an array so you can reference them later and for example easily check which tile some actor in your game is on. Also you probably need a way to determine the locations of county tiles etc beforehand so you can create the tilemap based on that data.
There's many things here that i think you could learn more easily by following some grid based game tutorial. Or a tutorial for a graphically tile based game, depending on what tou actually want from the tile-basedness. Otherwise you might have to ask about 50 more questions to get things going.
I think I have found another way to do it. Thanks anyway @Nose$$anonymous$$ills I will check out the tutorials though. Also The tiles would change owner... from one player to another based on if it is captured. Any ideas if you got them but I am pretty sure I got this one covered... I think.
Nothing special there. Just add
public int ownerId:
to your tiles... int
or whatever type you want to use to store the tile owner as.