- Home /
 
 
               Question by 
               Fox-Handler · Aug 07, 2014 at 09:57 AM · 
                ainavmesh  
              
 
              Smarter AI Searching for the Player
I've been trying to set up the enemy Ai to take the player's last known direction and use that for a way to check specific waypoints in the level depending on what that last direction was. Does anyone have experience with enemy AI searching for the player?
     void Chasing ()
     {
         // Create a vector from the enemy to the last sighting of the player.
         Vector3 sightingDeltaPos = enemySight.personalLastSighting - transform.position;
         
         // If the the last personal sighting of the player is not close...
         if(sightingDeltaPos.sqrMagnitude > 4f)
             // ... set the destination for the NavMeshAgent to the last personal sighting of the player.
             nav.destination = enemySight.personalLastSighting;
 
         Quaternion rotation = Quaternion.LookRotation(enemySight.personalLastSighting);
         if(nav.remainingDistance < nav.stoppingDistance)
         {
               //??????
         }
 
         if(lastPlayerSighting.position != lastPlayerSighting.resetPosition) // if the alarm goes off then chaseSpeed
         {
             nav.speed = chaseSpeed;
         }
         else if(lastPlayerSighting.position == lastPlayerSighting.resetPosition) //if the alarm doesn't go off then alertSpeed
         {
             nav.speed = alertSpeed;
         }
         
         // If near the last personal sighting...
         if(nav.remainingDistance < nav.stoppingDistance)
         {
             // ... increment the timer.
             chaseTimer += Time.deltaTime;
             
             // If the timer exceeds the wait time...
             if(chaseTimer >= chaseWaitTime)
             {
                 // ... reset last global sighting, the last personal sighting and the timer.
                 lastPlayerSighting.position = lastPlayerSighting.resetPosition;
                 enemySight.personalLastSighting = lastPlayerSighting.resetPosition;
                 chaseTimer = 0f;
             }
         }
         else
             // If not near the last sighting personal sighting of the player, reset the timer.
             chaseTimer = 0f;
     }
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
Can you use NavMesh agents to vary driving/handling model? 0 Answers
NavMesh Agent Obstacle Avoidance Ignore 1 Answer
How to add variables to Components (Nav Mesh Agent)? 0 Answers
How to make AICharacterController able to walk on walls and ceilings? 0 Answers
What’s the simplest way to find nearest navmesh targets? 0 Answers