Drag pathfinding grid prefab into scene hierarchy.
Set Pathfinding Grid Variables “Grid World Size”, “Unwalkable”, and “Node Width”
Grid World Size: X: 15, Y:0, Z:15
Unwalkable: Include layers “PhantomTech_Hidden”, and any other layers you wish to be detected as obstacles.
Node Width: 0.2
(Recommended that the Y position on the game object itself is set to somewhere between -1 and -1.5)
4. Attach “Pathfinding” component on to the same Game Object that you want to have use pathfinding.
5. In the Pathfinding component, drag your Pathfinding Grid game object to the variable slot named “Map”.
6. Set Pathfinding component variables “Speed” and “Rotation Speed”.
Rotation Speed: 5
(Target can be set here or left empty and set by a script with a reference to the pathfinding component)
7. Create a reference to the pathfinding component in your own script, on the object that you want to use pathfinding.
Before pathfinding can work we need to set a desired target destination. We can assign this through the Pathfinding.SetTarget() function. (We can also drag a game object into “Target” slot the Pathfinding component from the Unity Editor as seen above)
(An example Unity Start() function defining a pathfinding target by passing a Unity GameObject)
Pathfinding.SetTarget() has 2 overloads that allow for setting target either as a Vector3 position or a Unity GameObject. Passing a GameObject will allow pathfinding to follow an object even if its transform position changes.
(Note: Pathfinding always solves from our objects current position.)
There are two ways to Start/Stop pathfinding. We can explicitly define the pathfinding state with the following functions:
Alternatively, we can toggle the pathfinding state using the following function:
In order for the Pathfinding script to move your object, we must provide a move speed and a rotation speed. These values can be adjusted on the Pathfinding component in the Unity inspector.
Alternatively we can modify these values through script with the following fields:
If we do not want to have the Pathfinding script manage the movement of our object (for example, if we want the object to move via animation), we simply leave Pathfinding.m_speed at 0. We may still want to have a Pathfinding.m_rotationSpeed set to a value greater than 0.
PhantomTech.Pathfinding contains the following Booleans that allow us to check the current state of pathfinding:
m_atTarget – Use to check if pathfinder is at destination
m_isMoving – Use to check if pathfinder is currently moving. (Only applies if pathfinding script is managing the pathfinder movement.)
Pathfinding.GetPath() can be called to return an array of Vector3s.