NPC to seek or flee from a moving object
Rather than a destination that is fixed when the scene starts, let’s allow the Capsule-destination
object to be moved by the player while the scene is running. In every frame, we’ll get our NPC arrow to reset the destination of NavMeshAgent to wherever Capsule-destination
has been moved to.
Getting ready
This recipe adds to the previous one, so make a copy of that project folder and do your work for this recipe with that copy.
How to do it...
To make an NPC seek or flee from a moving object, follow these steps:
- With the
Capsule-destination
GameObject selected in the Hierarchy panel, add a Rigid Body Physics component to it by going to Component | Physics | Rigidbody. - In the Inspector panel for the
Capsule-destination
GameObject, check the Freeze Position constraint for the Y axis in the Constraints options of the RigidBody component. This will prevent the object from moving in the Y axis due to...