- Home /
How to create a grid in unity3d?
Hello all,
i am a unity game developer,and i m pretty bad with the grids. i have to create a prototype for cube movement in a grid. but i don't know how to create a grid. any thing like pseudo code or algorithm for creating a grid in unity will be very much helpful for me.
Thanks in Advance! Niki.j
Answer by save · Dec 26, 2012 at 08:48 AM
Hi Niki!
I got help with this once, the secret is to round off the number.
This script example moves a transform at a predefined direction each second within a grid:
var gridSize : Vector3 = new Vector3(1,1,1);
var movementDirection : Vector3 = new Vector3(0,0,1);
function Start () {
InvokeRepeating("UpdatePosition", 1.0, 1.0);
}
function UpdatePosition () {
var newPos : Vector3 = transform.position+movementDirection;
newPos = Vector3(Mathf.Round(newPos.x/gridSize.x)*gridSize.x,
Mathf.Round(newPos.y/gridSize.y)*gridSize.y,
Mathf.Round(newPos.z/gridSize.z)*gridSize.z);
transform.position = newPos;
}
Great script. Helped me a lot with my grid project, thanks!
Hey ! i dont understand where i have to put that script so when i make a cube or add a prefab i can use the grid to place it or scale/rotate it... also id like to know if i could turn the grid on/off. Thanks
Well you could just put it in a script called grid and attach is to your prefab. Then you could use a bool which controls enabled = true; to turn it on or off.
Thanks $$anonymous$$ike! Im trying to make some kind of prefab modules to build levels or maps for a game, so you have different corridors, rooms props etc and you can use the modules to build them however they want. Ill try this and see how it works out!
The code is correct, but can be easily tuned and simplified: divide, round and multiply could be very slow due to the floating point arithmetic. Insead, use this more intuitive C# script:
int snap( int value, int size ){
int remaining;
int halfSize; //can be cached somewhere
halfSize = size / 2; // bitwise operation for integer: size >>1;
remaining = (value % size); // where % is the division's remaining
if( remaining < halfSize)
return value - remaining;
else
return (value - remaining) +1;
}
.....
// remember to cache the gridSize's half values to optimize
newPos = Vector3( snap(newPos.x, gridSize.x),
snap(newPos.y, gridSize.y),
snap(newPos.z, gridSize.z),
);
I assumed that newPos.xyz are integer, but could be a float or a double. Just overload the snap method.
Answer by darkcookie · Dec 26, 2012 at 08:34 AM
I have a related question. How can you check the state of a specific grid space? I know that is a broad question, so here are a few examples: find a game object in a selected grid. maybe there is some type of terrain in that space that affects any game object occupying this space.
Answer by z25assassins · Apr 03, 2017 at 06:51 AM
try this video https://www.youtube.com/watch?v=Q0nbfq-f1os
Your answer
Follow this Question
Related Questions
Create a grid on a cube? 0 Answers
How to find sprite at grid location? 1 Answer
Grid-based building system not working 1 Answer
grid-based block placement system with touch input 1 Answer
2D Board game grid placement 0 Answers