• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by uss_enterprise_recruit · May 16, 2016 at 02:52 PM · c#stealth

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.

 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vittu1994 · May 16, 2016 at 03:58 PM 0
Share

Share your code so its easier to understand your problem

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Finding Boundaries of a Cover Object 1 Answer

Thief Like Stealth Systems in C#. Please Help!!! 3 Answers

Game Object Positioning 1 Answer

Two stupid questions about calling a method from another script (C#) 2 Answers

What should I use, a Dictionary with 100 entries or a class with 100 variables? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges