enemy not spotting player?
hi! i am having issues with my enemy on stealth. the player is patrolling, but when i trigger something it does not recognize me. any suggestions as to what the problem could be , and if possible, get back to me? many thanks.
using UnityEngine; using System.Collections;
public class EnemyAI : MonoBehaviour { public float patrolSpeed = 3f; public float chaseSpeed = 7f; public float chaseWaitTime =0.3f; public float patrolWaitTime = 0.5f; public Transform[]patrolWayPoints; //this determines the speed of the enemy guard as well as the waypoints, and the time taken for the guard to wait. //as these are public floats, they can be edited to go fast or slow or to wait for a longer or shorter amount of time. private EnemySight enemySight; private NavMeshAgent nav; private Transform player; private PlayerHealth playerHealth; private LastPlayerSighting lastPlayerSighting; private float chaseTimer; private float patrolTimer; private int wayPointIndex; //as well as the public variables, we need some private variables to call on other scripts. void awake() { enemySight = GetComponent(); nav = GetComponent(); player = GameObject.FindGameObjectWithTag(Tags.player).transform; playerHealth = player.GetComponent(); lastPlayerSighting = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent();
     //to start off with , it is important that the other scripts are called upon . this is done with the get component function.
 }
 void Update()
 {
  if (enemySight.playerInSight && playerHealth.health > 0f) 
     {
         Shooting ();
     } 
     else if (enemySight.personalLastSighting != lastPlayerSighting.resetPosition && playerHealth.health > 0f) 
     {
         Chasing ();
     }
     else
         Patrolling();
     //the update function contains loops for if the enemy is shooting the player, chasing the player or patrolling 
 }
 void Shooting()
 {
     nav.Stop();
     //if the enemy guard is shooting , it would need to stop
 }
 void Chasing()
 {
     Vector3 sightingDeltaPos = enemySight.personalLastSighting - transform.position;
     if(sightingDeltaPos.sqrMagnitude > 4f)
         nav.destination = enemySight.personalLastSighting;
     //for the chasing we would need the sightingDeltaPos function
     nav.speed = chaseSpeed;
     if(nav.remainingDistance < nav.stoppingDistance) 
     {
         chaseTimer += Time.deltaTime;
         if(chaseTimer >= chaseWaitTime) 
         {
             lastPlayerSighting.position = lastPlayerSighting.resetPosition;
             enemySight.personalLastSighting = lastPlayerSighting.resetPosition;
             chaseTimer = 0f;
             // if the player is spotted, the guard will go into chase mode
             }
          }
     else
     chaseTimer = 0f;
     // if the player is being chased then the chase timer is reset
 }
 void Patrolling()
 {
     nav.speed = patrolSpeed;
     if(nav.destination == lastPlayerSighting.resetPosition || nav.remainingDistance < nav.stoppingDistance) 
     {
         patrolTimer += Time.deltaTime;
         if(patrolTimer >= patrolWaitTime) 
         {
             if (wayPointIndex == patrolWayPoints.Length - 1)
                 wayPointIndex = 0;
             else
                 wayPointIndex++;
             patrolTimer = 0;
         }
     } 
     else
         patrolTimer = 0;
     nav.destination = patrolWayPoints[wayPointIndex].position;
      //these perameters will activate when the guard is patrolling , which is when the player is not in sight.
 }
}
Share your code so its easier to understand your problem
Answer by uss_enterprise_recruit · May 16, 2016 at 07:20 PM
public class EnemyAI : MonoBehaviour { public float patrolSpeed = 3f; public float chaseSpeed = 7f; public float chaseWaitTime =0.3f; public float patrolWaitTime = 0.5f; public Transform[]patrolWayPoints; //this determines the speed of the enemy guard as well as the waypoints, and the time taken for the guard to wait. //as these are public floats, they can be edited to go fast or slow or to wait for a longer or shorter amount of time. private EnemySight enemySight; private NavMeshAgent nav; private Transform player; private PlayerHealth playerHealth; private LastPlayerSighting lastPlayerSighting; private float chaseTimer; private float patrolTimer; private int wayPointIndex; //as well as the public variables, we need some private variables to call on other scripts. void awake() { enemySight = GetComponent(); nav = GetComponent(); player = GameObject.FindGameObjectWithTag(Tags.player).transform; playerHealth = player.GetComponent(); lastPlayerSighting = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent();
     //to start off with , it is important that the other scripts are called upon . this is done with the get component function.
 }
 void Update()
 {
  if (enemySight.playerInSight && playerHealth.health > 0f) 
     {
         Shooting ();
     } 
     else if (enemySight.personalLastSighting != lastPlayerSighting.resetPosition && playerHealth.health > 0f) 
     {
         Chasing ();
     }
     else
         Patrolling();
     //the update function contains loops for if the enemy is shooting the player, chasing the player or patrolling 
 }
 void Shooting()
 {
     nav.Stop();
     //if the enemy guard is shooting , it would need to stop
 }
 void Chasing()
 {
     Vector3 sightingDeltaPos = enemySight.personalLastSighting - transform.position;
     if(sightingDeltaPos.sqrMagnitude > 4f)
         nav.destination = enemySight.personalLastSighting;
     //for the chasing we would need the sightingDeltaPos function
     nav.speed = chaseSpeed;
     if(nav.remainingDistance < nav.stoppingDistance) 
     {
         chaseTimer += Time.deltaTime;
         if(chaseTimer >= chaseWaitTime) 
         {
             lastPlayerSighting.position = lastPlayerSighting.resetPosition;
             enemySight.personalLastSighting = lastPlayerSighting.resetPosition;
             chaseTimer = 0f;
             // if the player is spotted, the guard will go into chase mode
             }
          }
     else
     chaseTimer = 0f;
     // if the player is being chased then the chase timer is reset
 }
 void Patrolling()
 {
     nav.speed = patrolSpeed;
     if(nav.destination == lastPlayerSighting.resetPosition || nav.remainingDistance < nav.stoppingDistance) 
     {
         patrolTimer += Time.deltaTime;
         if(patrolTimer >= patrolWaitTime) 
         {
             if (wayPointIndex == patrolWayPoints.Length - 1)
                 wayPointIndex = 0;
             else
                 wayPointIndex++;
             patrolTimer = 0;
         }
     } 
     else
         patrolTimer = 0;
     nav.destination = patrolWayPoints[wayPointIndex].position;
      //these perameters will activate when the guard is patrolling , which is when the player is not in sight.
 }
}
Your answer
 
 
             Follow this Question
Related Questions
Thief Like Stealth Systems in C#. Please Help!!! 3 Answers
Finding Boundaries of a Cover Object 1 Answer
Game Object Positioning 1 Answer
Vector3 magnitude broken? 1 Answer
Not a programmer, but working on a game where I need to roll dice. 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                