- Home /
Recomend a build strategy
Hello,
It is a pleasure to join the community. This will be a beginner's question, but I will make it as not-noob as possible.
I understand C# and have no problems using it, but now I need to know how to implement my knowledge into unity.
I would normally watch tutorials or reed books, I will do that indeed, but time is a BIG factor now, I need this project done in a month.
Idea is:
I need to be able to implement an int[25,25] matrix into the game logic, and represent it visually, so that every cell of the array matrix is a square on the screen, where objects can be placed. (25x25 - 50 squares that will be arranged as e big square plane in unity)
I need to apply my rules for the game - there are 3 types of objects - a main base, a chicken and a pig, each taking exactly 1cell in both the matrix and the visual plane. There will be no movements, the base will be pre-placed and the chickens will be guarding it (they will be placed at certain positions).
The player will use the pigs by placing them on the plane (in certain squares that he finds beneficial) and trying to destroy the base and the chickens.
Let's say this is the int[25,25] matrix at the beginning of the game:
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000220000000000000
0000000020000000000000000
0000000203020020000000000
0000000000000022000000000
0000000022220000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
0000000000000000000000000
I need this to appear in the game in the form of a plane where every '0' is a square, every '2' is a square with a chicken object on it and every '3' is a base square. Now the player will be able to click on the squares and place pigs, which will be represented as '1's in the matrix, and they will either die or die killing the chickens and the base, depending on the game rules. Every time a pig is placed on the plane, there will be a loop that will be checking the int[,] matrix to see if the pig succeeded.
Now, How can I put this into Unity? I am thinking of the isometric view, and it will be something like a chess game kind of feeling. I need the squares to be the only places a game character can be placed, and to have something like a mouse over effect, so that the user can see where he can put the pig. I believe 2D will be more adequate since I will be using custom drawn sprites.
I don't want a complete source code of the problem, just give me a direction to follow. I have a month to finish this, so I need specific things to focus on, not the whole Unity for now.
Thank you very much for your time!
Answer by youngapprentice · Jan 25, 2013 at 02:17 PM
Welcome to the community! The 'for' loop is your friend here ;)
I just did something like this for an enemy spawning process in my game.
All righty. Well you'll need to familiarize yourself with some common Unity classes. Two things you'll definitely be using are instantiate and Vector3.
Instantiate spawns something at a given location with a given rotation, and Vector3 is a class that describes a point in space (X,Y, and Z) with Y being Up, Z being Forward, and X being Sideways.
Now, you have said you want this grid on a plane, so I imagine you'll want the Y axis to be uniform throughout.
What I would do is this:
Make an Empty GameObject and name it 'Grid'
Place it at the desired grid location
Make a script and attach it to the grid
The script would loosely follow this (I'm just handing you some math don't worry)
Oh and before we move on you should learn about transform. "Every object in a scene has a Transform. It's used to store and manipulate the position, rotation and scale of the object. Every Transform can have a parent, which allows you to apply position, rotation and scale hierarchically. This is the hierarchy seen in the Hierarchy pane. They also support enumerators so you can loop through children using:
// Moves all transform children 10 units upwards!
for (var child : Transform in transform) {
child.position += Vector3.up * 10.0;
}
" So you would have one variable for columns of type int, one variable for rows of type int, one variable for gridWidth in real world units of type float, and another variable of gridHeight in real world units of type float.
You would also have two non-exposed variables (this can be done by putting @HideInInspector on the line before the variable) that are deltaWidth and deltaHeight, the define the width and height between each point on the grid, respectively.
you would then make a 2D array called gridArray of type Vector 3. In UnityScript, you would define it like so:
var gridArray : Vector3[,];
Because things are being indexed, your deltaWidth would be equivalent to (gridWidth / ( columns-1 )) and your deltaHeight would be equivalent to ( gridHeight / ( rows-1 )). I recommend you do this math and cache these values in the Start() function.
you then construct a double-nested for-loop that goes through number of columns and rows, and for each one, assigns a new Vector3 to the current placeholder in the gridArray. My math for this went as follows:
gridArray[u,i] = new Vector3( transform.position.x + (deltaWidth*i), transform.position.y + ( -deltaHeight*u ), transform.position.z );
with u and i being the for loop placeholders.
Your grid is done! To offset the grid so that your empty gameObject actually exists in the center of the grid, add this (you don't have to if you want it to exist as the upper-left corner):
gridArray[u,i].x = gridArray[u,i].x - ( gridWidth/2);
gridArray[u,i].y = gridArray[u,i].y + ( gridHeight/2);
Now remember that in this code I am constructing a grid that is a flat plane running on the Y and X axes. You need to change the code so that it runs on the X and Z axes.
In your instantiation code, you would just run through it with a 'for' loop and spawn whatever prefab you want every x amount of columns / rows.
And now that I have given most of my code already, I am going to post the rest of it (there is also a useful debugging tool that draws the array for you. Be warned: it is in UnityScript. It is also annoying in the sense that when you change things in the editor, it spits out nasty warnings. I don't know how to fix that. ;)
Good Luck!
-YA
#pragma strict
@script ExecuteInEditMode
var debug : boolean;
var columns : int;
var rows : int;
var gridWidth : int;
var gridHeight : int;
@HideInInspector
var deltaWidth : float;
@HideInInspector
var deltaHeight : float;
@HideInInspector
var gridArray : Vector3[,];
function Awake () {
//Does the math to find the relative distance between points (Index-Based means we have to subtract 1 from column/row count)
deltaWidth = ( gridWidth / ( columns-1 ));
deltaHeight = ( gridHeight / ( rows-1 ));
gridArray = new Vector3[ rows , columns ];
for( var u = 0; u < rows; u++ ){
for( var i = 0; i < columns; i++ ){
//Constructing grid using the transform as the UPPER LEFT CORNER
gridArray[u,i] = new Vector3( transform.position.x + (deltaWidth*i), transform.position.y + ( -deltaHeight*u ), transform.position.z );
//These next two lines offset the points so that the transform is now used as the CENTER --(remove these if you want to use it as the corner)--
gridArray[u,i].x = gridArray[u,i].x - ( gridWidth/2);
gridArray[u,i].y = gridArray[u,i].y + ( gridHeight/2);
}}}
//Used to draw the grid in Editor
function DrawGrid(){
for( var q = 0; q < (columns); q++){
var column_top : Vector3 = gridArray[q,0];
var column_bottom : Vector3 = gridArray[q, (rows-1) ];
Debug.DrawLine (column_top, column_bottom, Color.red);
}
for( var w = 0; w < (rows); w++){
var row_top : Vector3 = gridArray[0,w];
var row_bottom : Vector3 = gridArray[ (columns-1), w ];
Debug.DrawLine ( row_top, row_bottom, Color.red);
}}
//If debug is active, draw the grid
function Update(){
if(debug) DrawGrid();
}
Thank you very much mate! I will dissect all the info you gave me, but even on first glance I see you gave me exactly what I needed to jump start this thing.
Cheers!
Good luck! If you have any more questions feel free to comment!
Your answer
Follow this Question
Related Questions
Legacy Animation Advice 0 Answers
How to force a script to affect only a single instance of the object it is applied to? 0 Answers
How to create a grid in unity3d? 3 Answers
Question about UI system in 3D game 0 Answers
Auto-move on grid and draw line problem 0 Answers