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 /
This question was closed Feb 10, 2017 at 05:55 PM by MickeyAnim for the following reason:

I found out myself

avatar image
0
Question by MickeyAnim · Feb 10, 2017 at 05:06 PM · playeraiwaitforsecondsenemyaisight

C# Enemy AI, when player is not visible i want the Enemy to chase the player for a few more seconds and then stop.

The enemy is sitting and if a player interacts with it it chases the player and if it cannot find the player in a few seconds the enemy will go sit again, the script works but i can't make the AI wait for a few seconds. any tips?

public class SittingEnemyAI : MonoBehaviour {

      [Header("Transforms")]
      public Transform ChairPoint;
      public Transform player;
      public Transform Front;
  
      [Header("Sight Range")]
      public float Range;
  
      [HideInInspector]
      public Animator anim;
      [HideInInspector]
      private NavMeshAgent agent;
  
      [HideInInspector]
      public bool Patrolling;
      private bool lost;
  
      void Awake () 
      {
          agent = GetComponent<NavMeshAgent>();
          anim = GetComponentInChildren<Animator> ();
          Patrolling = true;
      }
  
      void Update () 
      {
          if (Patrolling == true) 
          {
              lost = false;
              Lost ();
          }
          if (lost == true) 
          {
              StartCoroutine (StillLooking ());
          }
  
          //Debug.DrawRay (Front.position, Front.forward * Range);
          RaycastHit hit;
          Ray View = new Ray (Front.position, Front.forward);
  
          if (Physics.Raycast (View, out hit, Range)) 
          {
              if (hit.collider.tag == "Player") 
              {
                  Chasing ();
                  StopCoroutine (StillLooking ());
              } 
              else
              {
                  if (Patrolling == false) 
                  {
                      lost = true;
                  }
              }
          } 
          else 
          {
              if (Patrolling == false) 
              {
                  lost = true;
              }
          }
  
          if (anim.GetCurrentAnimatorStateInfo (0).IsName ("sit_to_stand")) 
          {
              anim.SetBool ("StandUp", false);
          }
          if (anim.GetCurrentAnimatorStateInfo (0).IsName ("Running")) 
          {
              Chasing ();
          }
          if (anim.GetCurrentAnimatorStateInfo (0).IsName ("Walk")) 
          {
              agent.destination = ChairPoint.position;
              Patrolling = true;
          }
  
          if (anim.GetCurrentAnimatorStateInfo (0).IsName ("looking_around")) 
          {
              agent.destination = this.transform.position;
              anim.SetBool ("StandUp", false);
          }
  
          if (anim.GetCurrentAnimatorStateInfo (0).IsName ("Typing")) 
          {
              transform.rotation = ChairPoint.transform.rotation;
          }
      }
  
      public void Chasing()
      {
          anim.SetBool ("Chase", true);
          agent.destination = player.position;
  
          agent.speed = 3.0f;
          agent.angularSpeed = 360.0f;
  
          Vector3 lookAtPosition = player.position;
          lookAtPosition.y = transform.position.y;
          transform.LookAt (lookAtPosition);
          anim.SetBool ("Sit", false);
          Patrolling = false;
      }
  
      void Lost()
      {
          anim.SetBool ("Chase", false);
          agent.speed = 0.5f;
          Patrolling = true;
      }
  
      void OnTriggerEnter(Collider collider)
      {
          if (collider.tag == "EnemySit") 
          {
              anim.SetBool ("Sit", true);
          }
      }
  
      IEnumerator StillLooking()
      {
          Chasing ();
          yield return new WaitForSeconds (3);
          Lost ();
          lost = false;
          Patrolling = true;
      }
  }





Comment
Add comment
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

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

129 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Enemy AI, when player is not visible i want the Enemy to chase the player for a few more seconds and then stop. 0 Answers

Trouble making jumping spider enemies 0 Answers

How to use IEnumerator? Pls help! 1 Answer

How do I stop colliders from going against each other / The enemy ai and player wont stop going against each other 0 Answers

Help with enemy AI 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