Heres a picture and below is the code I added.
[35049-screenshot+(75).png|35049]
using UnityEngine;
using System.Collections;
public class Levels : MonoBehaviour {
private int levelWidth;
private int levelHeight;
public Transform NcolWoodOneTile;
public Transform NcolGrassOneTile;
private Color[] tileColors;
public Color NcolWoodOneColor;
public Color NcolGrassOneColor;
public Color SpawnPointColor;
public Texture2D levelTexture;
public Entity Player;
// Use this for initialization
void Start () {
levelWidth = levelTexture.width;
levelHeight = levelTexture.height;
loadLevel ();
}
// Update is called once per frame
void Update () {
}
void loadLevel () {
tileColors = new Color[levelWidth * levelHeight];
tileColors = levelTexture.GetPixels ();
for(int y = 0; y < levelHeight; y++)
{
for(int x = 0; x < levelWidth; x++)
{
if(tileColors[x+y*levelWidth] == NcolWoodOneColor)
{
Instantiate(NcolWoodOneTile, new Vector3(x, y), Quaternion.identity);
}
if(tileColors[x+y*levelWidth] == NcolGrassOneColor)
{
Instantiate(NcolGrassOneTile, new Vector3(x, y), Quaternion.identity);
}
if(tileColors[x+y*levelWidth] == SpawnPointColor)
{
Instantiate(NcolGrassOneTile, new Vector3(x, y), Quaternion.identity);
Vector2 pos = new Vector2(x, y);
Player.transform.position = pos;
}
}
}
}
Is this what you want?
You can use the value tileSideLength after the game is running now to move them closer together / farther apart. It is not an exact solution but a simple one. Or you calculate the value depending on how big your rectangles are. The size of tileSideLength is simply the side length of your tiles/ little boxes. I didn’t test the example so it might not be 100% perfect.
using UnityEngine;
using System.Collections;
public class Levels : MonoBehaviour
{
private int levelWidth;
private int levelHeight;
public Transform NcolWoodOneTile;
public Transform NcolGrassOneTile;
private Color[] tileColors;
public Color NcolWoodOneColor;
public Color NcolGrassOneColor;
public Color SpawnPointColor;
public Texture2D levelTexture;
public Entity Player;
public float tileSideLength = 0.5f;
// Use this for initialization
void Start () {
levelWidth = levelTexture.width;
levelHeight = levelTexture.height;
loadLevel ();
}
// Update is called once per frame
void Update () {
}
void loadLevel ()
{
tileColors = new Color[levelWidth * levelHeight];
tileColors = levelTexture.GetPixels ();
for(int y = 0; y < levelHeight; y++)
{
for(int x = 0; x < levelWidth; x++)
{
if(tileColors[x+y*levelWidth] == NcolWoodOneColor)
{
Instantiate(NcolWoodOneTile, new Vector3(x, y) * tileSideLength, Quaternion.identity);
}
if(tileColors[x+y*levelWidth] == NcolGrassOneColor)
{
Instantiate(NcolGrassOneTile, new Vector3(x, y) * tileSideLength, Quaternion.identity);
}
if(tileColors[x+y*levelWidth] == SpawnPointColor)
{
Instantiate(NcolGrassOneTile, new Vector3(x, y) * tileSideLength, Quaternion.identity);
Vector2 pos = new Vector2(x, y) * tileSideLength;
Player.transform.position = pos;
}
}
}
}
}
The problem is that your tiles are not of unit size (1x1). When you drag your (presumably) sprites into the scene they will get a billboard or a sprite mesh to the size of the texture. If you simply scale your tile prefabs by 6.25 you should get them to line up. Attached a project for you to try out.[35154-new+unity+project+167.zip|35154]
If you wan’t it to do what minecraft does then use this
this is java script
#pragma strict
var spawnAreas : LayerMask; //Layers where you can spawn objects, make sure to fill this out
var destroyableObjects : LayerMask; //Objects that is ok to destroy, make sure to fill this out
var spawnPrefab : GameObject; //The object you want to spawn, in case you didn't assign the script creates a primitive
var grid : Vector3 = Vector3(1,1,1); //The grid you want to instantiate objects by
var colliderSize : Vector3 = Vector3(1,1,1); //The collider size of the object
var cam : Camera; //The camera you're using, in case you didn't assign one the script assigns the Main Camera
private var range : float = 100.0; //Don't use infinity, use the camera's far clipping plane if you need to
private var hit : RaycastHit;
function Start () {
cam = Camera.main;
}
function Update () {
if (Input.GetMouseButtonDown(0))
Build();
if (Input.GetMouseButtonDown(1))
Erase();
}
function Build () {
if (HitBlock(spawnAreas)) {
if (spawnPrefab==null) {
//In case you didn't have a prefab we spawn a primitive
spawnPrefab = GameObject.CreatePrimitive(PrimitiveType.Cube);
spawnPrefab.transform.position = Grid(hit.point);
} else {
//You have a prefab, let's spawn it
var spawnObject = Instantiate(spawnPrefab, Grid(hit.point), Quaternion.identity);
}
}
}
function Erase () {
if (HitBlock(destroyableObjects))
Destroy(hit.transform.gameObject);
}
function HitBlock (mask : LayerMask) : boolean {
var ray = cam.ScreenPointToRay (Input.mousePosition);
return Physics.Raycast(ray, hit, range, mask);
}
function Grid (pos : Vector3) : Vector3 {
return Vector3(Mathf.Round(pos.x/grid.x)*grid.x-(colliderSize.x/2), Mathf.Round(pos.y/grid.y)*grid.y+(colliderSize.y/2), Mathf.Round(pos.z/grid.z)*grid.z-(colliderSize.z/2));
}