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
-1
Question by Detinator10 · Aug 23, 2014 at 04:19 AM · coroutine

Coroutine does not work.

I have an enemy that charges at the player in my 2d platformer. I am using a co-routine attack() so that when the player walks into his range he waits for 0.5 seconds and then charges. However even after he starts waiting (WaitForSeconds) if the character jumps out of his range he will stop waiting and execute his main loop in FixedUpdate() and will walk, track the player, and other stuff even though anim.GetBool("attacking") == true. For some reason the animator variable attacking goes to false if the character jumps out of range even though I don't want it too. The only time I ever set attacking to false is at the end of attack(). Please help. Thank YOU Here is the code:

 public class EnemyChargerScript : MonoBehaviour {
     bool facingRight = true;
     public LayerMask whatIsGround;
     public LayerMask whatIsPlayer;
     float groundRadius = 0.2f;
     float attackRadius = 0.2f;
     public Transform GroundCheck;
     public Transform PlayerCheck2;
     public bool grounded = false;
     public bool Dead = false;
     public Transform onEdgeCheck;
     public bool onEdge;
     public Transform PlayerCheck;
     public float originalmove;
     float newmove;
     Vector3 BackupPosition;
     Animator anim;
     public GameObject Player;
     public bool dead;
     public PlayerControl playerscript;
     public GameWideScript gamescript;
     public bool activated = false;
     Vector3 inview;
     bool pushed = false;
     public int chargeSpeed;
 
 
 
 
 
 
     
     // Use this for initialization
     void Start () {        
 
         anim = GetComponent<Animator> ();
         anim.SetBool ("Attacking",false);
 
 
 
     }
 
     // Update is called once per frame
     void FixedUpdate () {
         grounded = Physics2D.OverlapCircle (GroundCheck.position, groundRadius, whatIsGround);
         onEdge = Physics2D.OverlapCircle (onEdgeCheck.position, groundRadius, whatIsGround);
         if (!grounded){
             this.rigidbody2D.velocity = new Vector2(0,0);
             this.transform.position = BackupPosition;
         }
 
         if (!onEdge) {
             BackupPosition = this.transform.position;
                 }
         if (!activated) {
                         inview = Camera.main.WorldToViewportPoint (this.transform.position);
             if (inview.x <= 1 && inview.x >= 0 && !activated) {
                 activated = true;
             }
                 }
 
 
         if(!anim.GetBool("Dead") && playerscript.Dead == false && !anim.GetBool("Attacking")  && activated)
         {
         if(Physics2D.OverlapArea (PlayerCheck.position,PlayerCheck2.position, whatIsPlayer, -20, 20))
                 Attack ();
 
         
                 
 
 
         if (this.transform.position.x < Player.transform.position.x){
                         newmove = originalmove;
 
             }
         if (this.transform.position.x > Player.transform.position.x){
                         newmove = originalmove * -1;
 
             }
 
         rigidbody2D.velocity = new Vector2 (newmove, rigidbody2D.velocity.y);
 
         anim.SetFloat("Speed",Mathf.Abs(newmove));
         if (newmove < 0 && !facingRight)
             Flip ();
         
         if (newmove > 0 && facingRight)
             Flip ();
 
 
         
 
         
 
 
 
 
         BackupPosition = this.transform.position;
             
         }
         if (playerscript.Dead == true) {
                         this.rigidbody2D.velocity = new Vector2 (0, 0);
             anim.SetBool("Pdead", true);
                 }
         
     }
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 
     void Attack()
     {
         Debug.Log ("enemyAttack");
         anim.SetBool ("Attacking",true);
         rigidbody2D.velocity = new Vector2 (0, 0);
         StartCoroutine("attack");
 
 
     }
     public void Die()
     {
         Debug.Log ("enemyDie");
         this.rigidbody2D.velocity = new Vector2 (0, 0);
         anim.SetBool ("Dead", true);
         Dead = true;
 
     }
 
     public IEnumerator attack(){
 
 
 
         yield return new WaitForSeconds (0.5f);
         Debug.Log ("kill");
         if (facingRight) {
                         rigidbody2D.AddForce (new Vector2 (chargeSpeed * -1, 0));
                 }
         else {
             rigidbody2D.AddForce(new Vector2 (chargeSpeed, 0));
                 }
         Debug.Log ("x");
         anim.SetBool ("Attacking", false);
 
     }
 
     void OnCollisionEnter2D(Collision2D collision){
         if (collision.gameObject.name == "character" && anim.GetBool("attacking") == true) {
                         playerscript.Die ();
                 }
                 
     }
     public void x(){
 
     }
 
 
 
 } 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by vinod.kapoor · Aug 23, 2014 at 05:25 AM

your coroutine is called every time the enemy is in range but you havn't put any condition to stop it. I think you should check once after the waitforsecond(), if the same condition holds for what you called the attack coroutine. If the condition is false you should use STOPCOROUTINE function to stop the execution of lines next to it

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 Detinator10 · Aug 23, 2014 at 05:34 AM 0
Share

I don't quite understand. The problem here is that I want my Co routine to, once started finish out even if the players not in range. What it does right now is that if the player jumps out of range it stops the Co routine and the enemy doesn't charge

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

23 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

Related Questions

How to hit game object in coroutine 2 Answers

SweepTest not working from Coroutine 0 Answers

Nested Coroutines: last coroutine quits early 1 Answer

Coroutine and Loops 2 Answers

IOS Touch movement not exiting coroutine 0 Answers


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