Turn Based Strategy / Grid Movement System

Hey all, just to clarify before I get started I’m a total Unity noob, literally just bought the engine yesterday after spending some time with the trial and tutorials. I like what I’m seeing, but the first project I’m thinking of leveraging it forward is not exactly what the engine was built for.

Basically, I’ve got a prototype for a turn based strategy game that was developed in another language super fast, and we’re looking to bring it in to a multi-platform engine, and hopefully one that can make it look pretty. We’re evaluating Torque for the task.

The biggest trick would be that the game is a turn based strategy game taking place on a grid. Now granted I’ve only been playing in the engine for a couple of days, but it’s pretty obvious such a game is not what Unity was intended for, though it certainly looks like it would be able to handle it. I’ve had some early success with laying out a grid of “tile” prefabs, which I can instantiate and then customize (say, this tile is a road, this tile is a forest, this tile is grass, etc) and the tiles can then receive mouseclicks, which I can use to figure out where the user is trying to click. It could work, but it doesn’t seem all that elegant so before I move forward I wanted to get some feedback from folks who have been working with the product longer than I have.

Just to clarify, a good way of framing up what I’m looking to do is by asking how you would implement a game of chess into Unity, specifically the board itself and how you would interact with it via mouseclicks.

Not really enough to picture the game exactly, but why don’t you have the board be one large textured object. You could simplify things by having a corner start at the origin so that positive X and Z values are all that you deal with. Then treat your interaction so that whatever your pieces are can only move to whole numbers on the X and Z axis. When you click on the “board”, cast a ray to see where you clicked in 3D space and treat the click as if it had occurred at whatever whole number those tiles are. So clicking at (13.23, 0, 8.31321) would be clicking on tile (13, 8). You could then have a multidimensional terrain array to classify tiles… (terrainType[13][8] = 5) where the value of the terrain influences whatever happens on the tile - movement penalties, whatever. Visually, it can look like whatever you texture it to look like.

I think that Unity would excel at this task, actually.

Well, if you can do it with Unity, then that’s what it was intended for. :wink: Unity generally doesn’t have any particular “path” unlike some engines. Unless it’s strictly 2D sprites, in which case I’d agree Unity’s not really intended for that. But even so, it’s doable, like this thing I did a while ago, which is 2D tile-based. Anyway, I agree with Charles about the method used; that’s essentially how I did the level editor for that project, although I used ScreenToWorldPoint instead of raycasting since it’s simply top-down 2D in my case.

–Eric

Yeah that solution sounds a heck of a lot more elegant than what I started doing. Before I run off experimenting with it, I just want to make sure I understand how to implement the solution you’re talking about with my limited understanding of Unity.

“Have a corner start at the origin” - So, if I’m understanding this right, if I wanted a board that was 10 x 10 squares, I would make a plane with scale of 10, 1, 10 and set its position to 5, 0, 5, which would put its “top left corner” at position 0, 0, 0.

Then using a Raycast (which I don’t know how to do yet, but I won’t bother anyone here about since it looks to be a pretty common element of Unity development), I find out where the user clicked, at which point I have the coordinates they clicked on my “grid”.

Sounds fantastic.

One other question in the meantime – what would be the best approach to take if I wanted to print the grid lines on the plane?

I’d probably use a projector for grid lines. For the sake of simplicity I’d make a plane of size 10, 1, 10 with the origin at 0, 0, 0; that way it could be extended to any size and you wouldn’t have to move it.

–Eric

Thanks guys, tried it out and this looks like exactly what I needed.

Hey all again I have a new question on the same project.

So now I’ve got my grid on a plane, I can create new units and I can have them move around beat on each other a bit. Now I’m back to a new display problem – how to highlight specific squares on the grid that are available for a certain action. To take examples directly from other games in the genre, when I select a Unit and choose to Move it, I want all of the grid spaces in that unit’s movement range to be highlighted in blue.

There was another topic about almost exactly this same thing back in October ( http://forum.unity3d.com/viewtopic.php?t=6984&highlight=grid ) but ultimately the fellow took another approach and the question of highlighting was never fully answered.

I was able to do a pretty crude implementation by placing colored planes in the boxes to be highlighted. It works, but

  1. I couldn’t get the planes to be transparent
  2. Everything else I’ve tried out so far can be translated to fields that aren’t perfectly flat ( I realize that projectors don’t project on terrains but that there are ways around that. On a side note, is there a way to move terrain after creating it, because it seems to be permanently stuck with a corner at 0,0,0). Using planes to highlight squares would leave me stuck with flat boards.
  3. Again, I don’t really feel like it’s the ideal solution, and I’m wondering what is.

Sorry if the questions are a bit noobish, but this is my first experience with Unity, or a 3D Game Engine of any kind really. Thanks in advance for any help offered.

I’d use a projector for that. It would be pretty simple to programmatically build a texture that contains the squares you want to highlight, and project that on the board. You should be able to make planes be transparent by using a transparency shader, but you’re right, planes are limiting. As far as moving a terrain, no, it stays at 0,0,0.

–Eric

Thanks for the fast reply. I tried to see how the projector approach would work by baking up a some-squares-blue texture in photoshop and it looks like it will work. But before I implement it fully, a couple of clarifications.

  1. When we’re talking about building a texture, would I just do that with SetPixel / SetPixels?
  2. …actually I had a question about shaders for this but I just answered my own question while dinking around.

So yeah, just that one – do I use Texture2D.SetPixel for creating those texture overlays?

Yep, SetPixels will do it.

–Eric