- Home /
Dynamic 2D Grid Building
How would I have building placement on a grid located on the deck of i.e. a ship. This ship can turn and move around, but the grid will always stay the same relative to the ship. Then, turrets or barracks can be built on this ship's grid and those buildings can be anywhere from 1x1 to 5x5. With the addition of multiple ships, this problem rattles my brain even more.
Are there any clean solutions to accomplish this? or are there any assets that will help me implement this into my game?
Checkout "Grids by GameLogic" It's a really fantastic asset for grids that will vastly simplify what you're trying to do.
Even using that asset, this will require quite a bit of custom code to make the grids work in the manner you want. It's no small task.
Still, it will be much easier than trying to make it from scratch.
Oh, thanks! I'd rather not break the bank until I know for sure it's the product I need.
I'm okay with making it from scratch. Just need some help brainstor$$anonymous$$g solutions. I was thinking of doing something with colliders but I'd much rather keep it all in code or it could get very messy.
It's a pretty robust product, and yes, you can create grids dynamically from script or with an editor.
One of the really cool things about it is the grids have two components, an efficient datastructure component, and a map component that controls how the data structure is mapped into world space.
What you're going to have to do to make it work is write a custom $$anonymous$$ap for your grid that will map the space relative to your ships, which can move around. It shouldn't be too bad.
If you are only doing Rectangle grids, there is a $20 version with just rect, and a $50 option with just Rect and Hex I bought the $100 version because I like having all the options but it's nice to be able to save money if you don't need all the weird grids.
I've used it on multiple projects, and I can attest to it's greatness.
I would recommend checking out their videos and forums also.
Well, the way to deal with the rotation is to get the location pointed at in world space, and then translate it into the ship's localspace using http://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.html
So then if your mast is (0,0,0) and your prow is (0,0,10) and you click on the prow, the inverseTransformPoint will give you (0,0,10) no matter what the rotation or position of the ship is.
Answer by DanSuperGP · Jan 16, 2015 at 01:05 AM
Well, the way to deal with the rotation is to get the location pointed at in world space, and then translate it into the ship's localspace using http://docs.unity3d.com/ScriptReference/Transform.InverseTransformPoint.html
So then if your mast is (0,0,0) and your prow is (0,0,10) and you click on the prow, the inverseTransformPoint will give you (0,0,10) no matter what the rotation or position of the ship is.
(Posted as an answer at the request of OP)
Awesome! From here, it's just implementing a simple 2D grid system with rounding for snap and matching the position returned to the grid's data system.
I just implemented the dynamic building system into my game and it worked beyond awesomely. Here's some of my code in case anyone wants to see how it works.
void Do$$anonymous$$ouseOver(){ //Only works if the mouse is over the grid; this is triggered by a bool in Update
RaycastHit hit;
float CastDist = (Camera.main.transform.position-(transform.position)).magnitude + (float)xLength/2 + (float)yLength/2;
if (Physics.Raycast(Camera.main.ScreenPointToRay (Input.mousePosition), out hit,CastDist, CastLayer))
{
CurX = $$anonymous$$athf.FloorToInt(transform.InverseTransformPoint(hit.point).x * xLength / 10);
CurX += xLength / 2;
CurY = $$anonymous$$athf.FloorToInt(transform.InverseTransformPoint(hit.point).z * yLength / 10);
CurY += yLength / 2;
print (CurX + "," + CurY);
}
}
Where xLength and yLength are the corresponding sizes of the grid.
Your answer
![](https://koobas.hobune.stream/wayback/20220613180558im_/https://answers.unity.com/themes/thub/images/avi.jpg)