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.

30 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.

      • I have found a solution to the “selectable” problem. For me Unity was not initializing my bool values in the class header, this could be because it wants then to be initialized on construction. The “walkable” variable was initializing to “false” even though i had “true” set in the class. I initialize my bool variables in the Start() method and “selectable” is setting just fine

  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!

        • I managed to solve this same error, in TacticsMove more on line 51 to 55 there is a Vector3 that was supposed to be -Vector3.

          Here the corrected code

                   if (Physics.Raycast (target.transform.position, -Vector3.up, out hit, 1))

          • I have a similar issue. I checked that very line and everything around it. Seems to match up but I still get the error NullReferenceException. This occurred at the very end of the tutorial and I’m too afraid to start from scratch.

  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).

      • I had the same problem and I solved it, I had an error in the
        tile script in the raycast part, i had if(Physics.Raycast ….) instead of if(!Physics.Raycast ….)
        hope this is usefull, sorry for the bad english

  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

  8. So I’ve gone through this a few times, starting from scratch and watching the video series again to really learn what’s going on here(new to Unity). I’ve decided to try and start making my own changes and the only thing I can really play around with is the jumping. I’m trying to make the character move forward at a faster pace and nothing I do changes the speed. I set moveSpeed to 10, 100, and even 1000. I already tried to mess around with the code that is normalizing the heading and the guy just stands still.

    Tried copying your code into Unity and mess with the moveSpeed and I’m still getting no results. Not sure what’s going on here, lol.

  9. Pingback: Dark Fantasy Tactics #4 #unity #gamedev #tutorial

  10. Fix for red tiles, in TacticsMove.cs in method GetTargetTile change Vector3 direction from up to Down
    public Tile GetTargetTile(GameObject target)
    {
    RaycastHit hit;
    Tile tile = null;
    if (Physics.Raycast(target.transform.position, Vector3.down, out hit, 1))
    {
    tile = hit.collider.GetComponent();
    }

    return tile;
    }

    • I had to do it both in GetTargetTile method in TacticsMove.cs and the CheckTile method in Tile.cs, but it also worked for me. Just putting this footnote in case it helps someone else

  11. Has anyone tried to incorporate a Main Menu scene prior? After the game loads, it has issues with the red tiles again, but still colors the current tile where Player starts on.

  12. I don’t know if some of you are still looking for solutions because of red tiles not showing, but try put your player gameobject a bit higher, worked instantly for me 🙂

Comments are closed.