We have created AI system by using NavMeshAgent in the previous articles. In this article, we will use the same controller as Player to make enemies moving.
The idea of making enemies moving by its Character Controller is locating where Player is, and trying to approach. This would be quite similar to using NavMeshAgent, and the difference would be there is no area limit to Character Controller.
You can make Character Controller walking through almost everywhere.
Let’s assume that we all have the object of enemy.
Add Character Controller and remove the collider.
Then created a script to handle the enemies movement, and get the reference of Character Controller and Player.
Inside Update(), first we need to calculate the direction of Player.
To prevent enemies snapping to Player within 1 frame, we added “normalized” to downsize the calculation result.
Then we need to limited the Y-axis position to prevent it leaning. And also use “direction” as the rotation value to facing Player.
Now we can get the velocity by using direction times speed. And make it affected by gravity if it is not on the ground.
After all these, the final step is making him walks to Player.
And now you have got an auto-chasing enemy.