in this video I'll be showcasing my attempt on a ti/ grid based movement in 2D you can see here a TI selection game object is hovering over each tile based on where the mouse is once I click on the tile the player will move towards it calculate the quickest way to reach this destination if you guys just want the project you can go ahead on my patreon and get it there I'd like to first thank my patreon supporters Jason long Muhammad Lan and Nicholas with that being said let's dive right into the video the way
I have my scene set up is with two to maps one being an obstacle Time map which the player will avoid and will have a rigid body 2D a composite collider 2D and obstacle Time map script the other one is a normal Tom map which will be used to decorate the scene with a tag Tom map I also have a game object prefab with a t selection script the material is semi-transparent so that the ti you're hovering is still visible the last thing I have in the scene is a player game object with the mid
script rigid body and circle collider this is the grid util script all it has is a public static Vector to in called world to grid which stores a world position and all this does is Returns the floor to in of the world pause X and World pause y the same concept for the other static Vector 2 called grid to world you don't need to attach this script to anything the same applies for the AAR script I have a private static Vector 2 called grid size at the top and I've also written a private class called
node we'll also have a reference to this parent node pathf finding cost G cost and H cost and whether it's an obstacle the f- cost is calculated as a value combining G cost and H cost which helps determine the best path to take moving down to the public static F path this is the bread and butter of the script in here it uses the AAR pathf finding algorithm to find a path from the starting point to the Target point on the grid it opens and closes a list to explore the grid positions and calculates the
cost which then decides the best path while avoiding obstacles Define by the is obstacle method the algorithm will continue to find the path to the Target giving back the best route and if it's shown null that means no path is found in the get lowest F cost node it looks at a list of nodes and chooses the one with the lowest combined cost making sure to prioritize that path which is closer to the destination when calculating the H cost it checks how far the player is from the targeted destination in the grid whether we should
be moving horizontally or vertically and ignoring moving diagonally as for the generate path method it connects points from the start and the end destination pretty much converting the grid position to the real world positions think of it has instructions for it to move on one node to another seamlessly lastly the get adjacent position this is basically straightforward like I said just adding up down left and right as its coordinates to move in its position that pretty much gives a brief summary of what each method does in the AAR script moving on to the T selection
script make sure you have usiing Unity engine. to Maps at the top the variables we'll need are Time map called Time app another toap called obstacle Time map a float called set a private Vector 2 named grid size which will be a new Vector 2 storing one in both X and Y and a private Vector 2 in called highlighted tile position which is set to Vector 2 in zero in the update method it constantly checks the mouse position in the game world and highlights a grid under the mouse if the mouse is not over an
obstacle you'll update the highlighted tole position and move the player to that position in the grid onto the highlighted position method it rieves the position of the currently highlighted tile which then helps us see which tile is being selected the public bolt is highlighted tile click helps determine if the player has clicked on the highlighted tile all it does is check if the grid position and the click position matches the Highlight tile's position also add a get highlighted tile position which is called when you want to place or interact with an object on that tile
lastly the bll isti obstacle which was called in the AAR script checks if the grid position contains an obstacle by seeing if there's a collider at that position looking at the obstacle Time app now once again make sure you're using Unity engine. time apps and for the variables we'll have a private toap called toap and a private hashset called obstacle toap position set to a new hashset in the awake method it ensures that the to map is set and it initializes a collection of positions where the obstacle towels are located within the map moving down
to initialize obstacle tiles it clears the collections of obstacle to positions and scans the entire map to find and record the positions of all the tiles that are not empty it then stores the positions in a collection for quick access later and lastly the is UPS the method this method helps you check if a given World position corresponds to an obstacle tole within the tole map it converts the world position to a grid position and then it confirms if it matches any of the obstacle time at position that it was stored in the last script
we'll be looking at is the character movement We'll add a public enum at the top called move Direction which will be up down left and right and inside our grid movement have a private float called move speed a private float called Vector 2 a private Vector 2 called grid size a reference to the obstacle to map called obstacle to map as well as a reference to the to selection have a private Vector 2 called Target position another private ball called is movement and the move Direction called current direction which is set to down for the
update method we'll just call the handle movement input and in here it constantly checks if the user and input handles cause the character movement when the player clicks on a tile and it's not an obstacle it will initiate a pathf finding process to reach the click the location for the fine path to Target position this finds a path from the character's current position to The Click Target tile and sets the character in the motion of an optimal path next on to the I numerator Move Along path this method smoothly moves the character along the desired
path it takes care of moving through their way points in the path and having the character state in between them lastly the move towards Target this method determines the character facing the direction ensuring that the target faces the right way when moving that pretty much covers all the scripts back in unity make sure you have the proper game objects scripts Etc attached to the inspector you can obviously copy some variables but make sure to double check each slot when I press play you can see the player being able to hover over tiles and when pressing
the left Mouse button the player will calculate and move towards the location using the a star pathf finding method to figure out which way is the quickest anyways that is all for this video if you do want the project you can go ahead by subscribing to my patreon down below and if you have any questions suggestions or just want to join my community my Discord will also be down in the description thank you guys for watching and I'll see you all in the next one bye