# Make NPC follow player when seen?

Hi all, I am making a horror game with a random walking NPC.
When the NPC sees the character, (when the player goes in range of the NPC’s attached box collider), I want the NPC to start following the player for 10 seconds.

for the code are at the bottom of my script, the rest is the random walking I coded.

``````public class Walking : MonoBehaviour {
private float latestDirectionChangeTime;
private readonly float directionChangeTime = 3f;
private float characterVelocity = 2f; //Random.Range(.5f, 3f);
private Vector3 movementDirection;
private Vector3 movementPerSecond;
public GameObject alien;

void Start(){
latestDirectionChangeTime = 0f;
calcuateNewMovementVector();
}

void calcuateNewMovementVector(){
//create a random direction vector with the magnitude of 1, later multiply it with the velocity of the enemy
movementDirection = new Vector3(Random.Range(-1.0f, 1.0f), 0,Random.Range(-1.0f, 1.0f)).normalized;
movementPerSecond = movementDirection * characterVelocity;
}

void Update(){
//if the changeTime was reached, calculate a new movement vector
if (Time.time - latestDirectionChangeTime > directionChangeTime)
{
latestDirectionChangeTime = Time.time;
calcuateNewMovementVector();
}

//move enemy:
transform.position = new Vector3(transform.position.x + (movementPerSecond.x * Time.deltaTime), 22.45f,
transform.position.z + (movementPerSecond.z * Time.deltaTime));

//wait a set amount of timeeeeeee
//if his movement is to the west, then he turns 90 in the y axis

alien.transform.rotation = Quaternion.Slerp (alien.transform.rotation,
Quaternion.LookRotation (movementDirection), Time.deltaTime * 4f);

// Update is called once per frame
void UntilSeen()
{
/*
* When the player is 'seen' (goes into the range of the box collider on the NPC)
* The NPC will follow the player at 2x speed
*
* If the player gets out of the NPC's box collider for 10 seconds,
* the NPC will slow down and go back to the Walking Script
*
* i know i need to use waituntil code
*/
}}}}
``````

I attached an image of that the box collider will look like.
I have walls in the game, and want to make sure that the collider cannot “see” through walls also. Thank you so much in advance!

You could try to use navmesh agents and when you want it to stop moving just use
NavMeshAgent.ResetPath()

Try this:

``````//Create a few more variables
private GameObject player;
private Transform target;
private float distance;
public float sight;

//Inside Start:
player = GameObject.FindGameObjectWithTag("Player")
target = player.GetComponent<Transform>();

//Inside Update:

distance = Vector3.Distance(target.position, alien.transform.position);

if (distance <= sight) {
alien.agent.SetDestination(target);
}
``````

You should be able to figure everything else out. Make sure you set your ground to static and under navigation click the bake button. Also add a NavMeshAgent into the enemy.

Maybe you can set the collider to trigger then add a

``````void OnTriggerEnter(Collider other)
{
// Start Following Player by getting the player coordinate in Update()
}
``````