To make an NPC seek or flee from a moving object, follow these steps:
- In the Inspector window, add a Rigid Body Physics component to the Capsule-destination GameObject.
- In the Inspector window for the Capsule-destination GameObject, check the Freeze Position constraint for Y-axis in the Constraints options of the RigidBody component. This will prevent the object from 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 the Capsule-destination GameObject:
using UnityEngine;
public class SimplePlayerControl : MonoBehaviour {
public float speed = 1000;
private Rigidbody rigidBody;
private Vector3 newVelocity;
private void Start() {
rigidBody = GetComponent<Rigidbody>();
}
void Update() {
float xMove = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
float zMove = Input.GetAxis("Vertical"...