Unity Tutorial – Tactics Movement

Tactics games, like Final Fantasy Tactics and Ogre Tactics, are turn-based combat games on square tiles. This multi-video tutorial will cover movement for a tactics game.

Player movement uses Breadth First Search (BFS) to calculate the tiles that the player’s unit can move to. NPC movement uses A* to calculate a path to the nearest player unit, and then limits the path to the unit’s maximum movement.

Part 1 – covers how to set up the tactics game board
Part 2 – covers how to set up the adjacency list for each tile
Part 3 – covers Breadth First Search (BFS) for calculating the tiles the player can move to
Part 4 – covers movement of the unit’s from tile to tile, including jumping up and down
Part 5 – covers the turn manager so the player and NPC can take turns moving
Part 6 – covers A* and NPC movement which chases after the player

Download this Unity tutorial

Videos

Part 1 – covers how to set up the tactics game board

Part 2 – covers how to set up the adjacency list for each tile

Part 3 – covers Breadth First Search (BFS) for calculating the tiles the player can move to

Part 4 – covers movement of the unit’s from tile to tile, including jumping up and down

Part 5 – covers the turn manager so the player and NPC can take turns moving

Part 6 – covers A* and NPC movement which chases after the player

Posted in AI, Artificial Inteligence, Code, Game Programming, Tutorials, Unity 3D.

17 Comments

  1. I have a problem with part 3 : my code is identical to yours, but my tiles dont become red : they’re not “selectable”, but the “current” tile (the pink one) is displayed both current AND selectable. I dont find where i made a mistake

    • Same exact problem. Using Unity 2018.4. The YT comments on that video suggested making sure the “if (!Physics.Raycast(tile.transform.position, Vector3.up, out hit, 1))” is correct, and it is for me. The other thing to look for is making sure each tile gameObject has the Tile script and tagged as Tile, which they are. The Main Camera is tagged as Main Camera, and the Player is tagged as Player.

  2. I’m trying to make it so if player doesnt use full moves he can move again with left over moves. I have it working so the player moves say 3 spaces and then the selectable tiles shrink to 2 spaces properly, but when I move the last 2 spaces and ends turn the NPC goes into a turn loop. I cant figure out how to make NPC stop at its 5 spaces to end turn

  3. So if we follow along with these videos and make a game based off of what we did, are we allowed to sell it? even if some of the scripts are unchanged?

  4. I followed the entire tutorial and I keep getting an error…

    “NullReferenceException: Object reference not set to an instance of an object
    TacticsMovement.GetCurrentTile () (at Assets/Scripts/TacticsMovement.cs:45)
    TacticsMovement.FindPath (TileScript target) (at Assets/Scripts/TacticsMovement.cs:330)
    NPCMove.CalculatePath () (at Assets/Scripts/NPCMove.cs:41)
    NPCMove.Update () (at Assets/Scripts/NPCMove.cs:28)”

    I have everything tagged and named correctly, but I continue to get this error. Please help, thank you!

  5. I know it’s 2020 and this tutorial was done several years ago. I’m trying to figure out what I’m doing wrong. I’ve written the code all the way up to Video 3 where you find the range of the players movement. I’ve seen comments on it not working on YouTube but none of the ideas are working for me. Now, for testing sack I downloaded your tutorial files to see if it was my version of Unity since I’m running the latest version. Your files still work so I’m a little confused as to why mine don’t work.

    I’m working on a game, that isn’t necessarily “Tactical” but it focus’s on the A* Pathfinding to move. All I’m trying to figure out is a better way to do the Pathfinding and pull the current units movement range since each unit has their own specific range. I know you wrote it in MoveTactics but I’m curious as to how to reference the current selected unit’s “distance”. There’s a lot in this comment, sorry. What I’m thinking of is probably more complicated than this tutorial. Just trying to figure out how.

    • I hear tou. I Tried all the sugestions in all the coments. Copied 5 times in the last week. I was hoping the creator would figure this out and help us out, since lots and lots of people are asking about the red tiles …. please help!!!

      • I had this issue as well. To solve you might need to attach the visual studio debugger to unity, add some breakpoints, and step through with f5/f10/f11. Verify the control flow is happening correctly, and the data in the variables is correct (mouse over or add a watch). For instance, add one at the end of FindNeighbors, and mouse over the adjacent tiles list. Check you are finding the correct adjacent tiles for a few tiles. It should find 2-4. 2 for a corner, 3 for the other edge tiles, and 4 for tiles in the middle. Or less if there are other objects on the grid. For me when we did the raycasting to locate them, he says it should be half the size but can be lower and uses .25. I used .5 and was finding 20 or more adjacent tiles. All the functions should be getting called. Make sure you are initializing and resetting tile data.

        For the red tiles not showing my main problem was in find selectable tiles, the breadth first search. There are three tile variables at play; currentTile, the tile coming out of the process queue, and the iterator for looping through the adjacent tiles. Make sure you are not mixing them up. I had tile.distance = 1 + tile.distance, instead of = 1 + t.distance, and a similar problem assigning the parent.

        Also the code uses the method GetObjectsByTag, so make sure all your grid objects have been assigned the tag ‘Tile’ (which you must create). Thankfully unity lets you select and assign many at a time with shift+click if you have to do this after the fact. Another issue I ran into was when I changed a default setting in code, the unity object retained the old value in the editor, so did not change. Click a few grid blocks and make sure walkable is checked in the inspector. You could also use the inspector rather than the debugger to see if current, target, and selectable are getting set for the appropriate blocks while the game is running (if you made these variables public).

  6. I hear tou. I Tried all the sugestions in all the coments. Copied 5 times in the last week. I was hoping the creator would figure this out and help us out, since lots and lots of people are asking about the red tiles …. please help!!!

  7. Hello.

    Need help, im at part 4, 13:02, Error, my guy wont move at all, no error he just wont move, i followed what he did, every step , i dont know how to do this HELP…….. T_T

Leave a Reply

Your email address will not be published. Required fields are marked *