Making NPCs Follow The Player

For various reasons, today I am working on a prototype based on Crowd City instead of my space shooter. As such, I thought I’d write about the NPC movement.

Note: This code is a work-in-progress, and I will be improving it tomorrow.

Today’s Objective: Make the NPCs follow the Player once recruited into their faction. They will try and maintain a general distance from the Player.

In the NPC script:

  • Two floats to control the distance the NPCs try and stay away from the player.
  • A boolean to indicate if the NPC has been recruited or not.
  • A Transform for the target the NPC will follow.
  • A reference to the Rigidbody component on this NPC.
In Awake() and Update()
This goes inside the Update’s _followTarget null-check brackets

That’s it for today’s quick post. Hope it helps someone.



Vincent Taylor

Unity game developer / C# Programmer / Gamer. Australian (Tasmanian) indie games developer for 10+ years. Currently looking for games industry employment.