Okay, so I just started learning Unity3D a couple of weeks ago and for my first project I decided to make a remake of Konami's old arcade game Frogger. I'm having trouble with my initialization script. Basically, as you probably know, Frogger moves accross a grid in this game. I figured the easiast way to do this was to write a script to instatiate a grid object at every 1 unit in the X and Z direction for a certain size. That's a pretty poor explanationg but basically I decided to make a grid. each square in the grid could then be set up as a waypoint for the player in the locomotion script. However, I'm having some strange problems with my code. For some reason it's creating duplicates of all the points in the grid and also having them offset by 1. For example, if my X position variable is 0, it for some reason spawns the object at x=1. below is my script.
var GridXMax : int;
var GridZMax : int;
var GridXMin : int;
var GridZMin : int;
var tile : Transform;
private var placeX:int;
private var placeZ:int;
var i : int;
private var positionX:int;
private var positionZ:int;
function Start () {
i=0; //counting variable
placeX = GridXMax - GridXMin; //finds the width of the grid
Debug.Log("grid will be "+ placeX + " coords wide");
placeZ = GridZMax - GridZMin; // finds the depth of the grid
Debug.Log("grid will be " + placeZ +" coords long");
while(i<placeX*placeZ)
{
positionX = ((i%placeX)); // finds the x position of the tile
positionZ = Mathf.FloorToInt((i)/placeX); //finds the z position of the tile
Debug.Log(positionX+", "+positionZ);
Debug.Log(i);
Debug.Log("n/");
Instantiate(tile, Vector3(positionX, 0, positionZ), Quaternion.identity); //spawns a tile at positionX and positionZ location, or rather is supposed to
tile.name = "Tile " + positionX + ", "+ positionZ; // names the newly spawned tile
i++;
}
}
I'm new to programming but I have done a lot of reading in the past few weeks and don't mind doing more, so if you see something wrong or off other then the problem I'm currently having I would very much appreciate links to some pages to read. I've also done my best to comment on my code I hope it is adequate. Thanks in advance!