Pathfinding in Unity3D Continued: Moving
In my last blog post I talked about how to find a path from a start position to an end position. Now let us use that path to move the entity to that end position.
Following The Path
First thing you need is a new component, this will be responsible for moving our entity around based on the given path. It is important for this to be a component because it needs to be updated every frame. The entity will slowly inch its way to the next tile and on to the next until it finally reaches the end position.
Now there are a couple ways of doing this, I felt like it was easiest to add the
Walk component when needed and remove it when finished. It looks
This component accepts a path and a completion method. It simply grabs tiles, one at a time from
the path and slowly moves the entity in a straight line from the last tile to the next
tile using Unitys built in method
On every update call, it checks to see if the entity has reached the next tile, if so it will attempt to find the next tile to move to. Or if it reached the final tile, finishes its walk and calls the completion method.
If the entity has not reached a tile yet, it will continue moving along in a straight line. Remember the entity is walking and animating in this time.
NextDirection method takes the last element out of the path and
converts its tile position into world space so a correct distance value can be
found. This just multiplies each number by the tile size.
This distance is then put into the
Vector3.Lerp method. This
method basically slowly increments in value based on time passed. You can read
more about it here.
Before the entity is first moved, a value
Speed is set on the
animator object. This signals that the entity is moving and the walking
animation is played. I think this will be refactored out in the future, it is
unrelated to the
OnVisit is called on every new tile entered. It will
trigger a global event, like I talked about in Unity Event Manager. This event can then be used to trigger things on the outside like, cutscenes, animations and AI. It can even be used to check for a hidden mine and explode.
Aligning to Tile
You might have noticed a component called
TileAlign being used in
the update method. As the entity moves around it will enter tiles with various
heights. This method provides an easy way of figuring out the height of the tile
directly below the entity. That value can then be used to move the entity up or
down so it is correctly standing on it.
This uses a ray cast that shoots directly below the entity and hits the tileColldier, which is specifically created to handle collisions on the map. A ray cast works wonders because it works in every situation, even on uneven tiles, like slops. This still work even if the entity is on the corner of the tile, because a ray cast is sent from the entities current position.
If a hit was made, its y value is then taken and returned as the tile height.
Walk component is used like so. First a path is found using the
path finding algorithm I talked about in the last blog post. Second a walk
component is added to the target which you want to move through the path.
Finally the method
FollowPath is called with the path and callback
The callback method is needed because this is an asynchronous process. When the
walking is complete, the
Walk component can be removed.
In the next blog post I will go through how to highlight individual tiles like in Final Fanytasy Tactics.