- Home /
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. The comments for the code are at the bottom of my script, the rest is the random walking I coded.
 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!
using a box collider is probably the wrong approach. Try using a Raycast ins$$anonymous$$d (it's even more intuitive for things like crouching behind objects, seeing the player through peep holes, etc.) whereas a collider will always prove somewhat troublesome.
 //pseudo-code  
 
 Update()
 
 //get angle from Alien's forward direction to the player's position
  Vector3 targetDir = player.position - transform.position;
         float angle = Vector3.Angle(targetDir, transform.forward);
        if (angle < 30f)
       {
        // limiting the field of view will imitate a "zone" in which he can see in front of him.
 // set a distance for him to look, so he can't catch the player infinity miles away
   if (Vector3.Distance(player.position, transform.position) < 15f)
         {
            TryFindPlayer();
            }
       }
 
 TryFindPlayer()
 {
 Vector3 directionToPlayer = (player.position - transform.position).Normalize();
 
  if (Physics.Raycast(transform.position, directionToPlayer, out hit, maximumViewDistance)
         {
             Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * hit.distance, Color.yellow);
             Debug.Log("Player has been spotted");
         }
 }
Answer by LightMaster905 · Feb 27, 2020 at 08:08 PM
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.
Answer by Triggerly · Feb 28, 2020 at 02:34 AM
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()
 }
 
Your answer
 
 
             Follow this Question
Related Questions
Make an NPC follow player? 1 Answer
Having a friendly character follow player 0 Answers
NPC code, works great, besides them being in the ground. 2 Answers
Sidescroller NPC follows Player 0 Answers
AI Enemy Follow Player 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                