Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by xXSLUGSXx · Jul 01, 2014 at 01:28 PM · c#animationai

Coroutine to WaitForSeconds() on enemy AI causing all instances of AI to pause. Solution?

I have two zombie prefabs instantiated right now, both with the following script in the prefab.

 void Update () {
         player = GameObject.FindGameObjectWithTag ("Player");
         allies = GameObject.FindGameObjectsWithTag ("EnemyOther");
         allyCount = allies.Length;
 
         float distance = Vector3.Distance (transform.position, player.transform.position);
         Debug.DrawRay (new Vector3(transform.position.x, transform.position.y  + 1, transform.position.z), (player.transform.position - new Vector3(transform.position.x, transform.position.y + 1, transform.position.z)),  Color.red);
 
         if (Physics.Raycast (new Vector3 (transform.position.x, transform.position.y + 1, transform.position.z), (player.transform.position - new Vector3(transform.position.x, transform.position.y + 1, transform.position.z)), out hit, maxRange, 9));
         {
 
             /*for (int i = 0; i < allyCount; i++) 
             {
                 if (Vector3.Distance (allies[i].transform.position, transform.position) < maxRange && distance >= 1.7 && !attacking)
                 {
                     bool pursuing = allies[i].GetComponent <ZombieAI>().pursuing;
 
                     lastKnownPosition = player.transform.position;
                     
                     transform.LookAt(new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));
                     transform.position += transform.forward * moveSpeed * Time.deltaTime;
                     animation.CrossFade("run");
                 }
             }*/
 
             if(hit.transform.tag == "PlayerOther" && distance >= 1.7 && !attacking)
             {    
                 lastKnownPosition = player.transform.position;
                 
                 transform.LookAt(new Vector3(player.transform.position.x, transform.position.y, player.transform.position.z));
                 transform.position += transform.forward * moveSpeed * Time.deltaTime;
                 animation.CrossFade("run");
             
             }
 
             if(hit.transform.tag == "PlayerOther" && distance <= 1.8 && !attacking)
             {
                 attacking = true;
                 StartCoroutine (AttackAnimation());
             }
     
             if (hit.transform.tag != "PlayerOther")
             {
                 transform.LookAt (lastKnownPosition);
                 transform.position += transform.forward * moveSpeed * Time.deltaTime;
                 animation.CrossFade ("run");
             
             }
         }
     }
 
     IEnumerator AttackAnimation()
     {
         animation.CrossFade ("attack1");
         yield return new WaitForSeconds(animation["attack1"].length);
         PlayerStats.Health = PlayerStats.Health - 10;
         attacking = false;
 
     }
 }
 

When one zombie gets close enough to attack, all zombies stop in their tracks as well rather than only the one who is close enough. This is my first time using a Coroutine, can I get some pointers? Thanks

Comment
Add comment · Show 3
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 HarshadK · Jul 01, 2014 at 01:32 PM 0
Share

How about using 'this' keyword for accessing only the current zombie to which this script is attached that you want to pause?

avatar image Bunny83 · Jul 01, 2014 at 01:44 PM 1
Share

@Harshad$$anonymous$$: That's pretty pointless since Update and the coroutine run in the local scope of the class. You already accessing the variables on this script by default. You only have to use "this" when you have another variable inside the local scope of a method with the same name as a member of the class. In that case the local variable is used first. To access the member variable you have to use "this".

avatar image HarshadK · Jul 01, 2014 at 01:54 PM 0
Share

@Bunny83 didn't know that. 'this' has always confused me right from the beginning since I started learning Java (the real Java) and then shifted to c# after started using Unity. And it still keeps on confusing me. Can you provide me some good reads/articles for understanding that?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jul 01, 2014 at 01:44 PM

Is it possible that you have declared "attacking" as static variable? In that case it is shared across all instances since there is only one "attacking" variable, no matter how many instances you create. Variables which should be per instance must not be static.

Since you didn't include the declaration of "attacking" it's just a guess. However if you have strange interaction between different instances that's usually caused by some hidden global state (like static variables).

Comment
Add comment · Show 1 · 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
avatar image xXSLUGSXx · Jul 01, 2014 at 04:23 PM 0
Share

Wow that was it lol, can't believe I missed that. Thanks XD

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to select an object with TOUCH and change its animation 2D 1 Answer

C# multiple AI Waypoint tutorial 0 Answers

Quaternion To Matrix conversion failed... 0 Answers

adding animation dynamically by script 0 Answers

Problems when i have multiple enemies on the same scene C# 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