Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges