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 NavMeshAgent's
destination 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:
- In the
Inspector,
add aRigid Body Physics
component toGameObject Capsule-destination
. - In the Inspector, for the
Capsule-destination
GameObject, check theFreeze Position
constraint for the Y axis in theConstraints
options of theRigidBody
component. This will prevent the object moving in the Y-axis due to collisions when being moved. - Create the
SimplePlayerControl
C# script-class, and add an instance-object as a component to theCapsule-destination...