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 theRelation · Oct 18, 2014 at 09:19 PM · yieldwhile

IEnumerator while loop for charge weapon error.

The chargeDmg float doesn't accumulate while fire-button is held down. I've tried editing the IEnumerator different ways and the only way I can get it to work is this way, but am I just returning null instead of the time.Deltatime value to chargeDmg? Is it more efficient in C# to separate methods that must happen simultaneously at run-time into events, members, properties, etc.? I've just started coding so I don't think I"m quite there yet...

 using UnityEngine;
 using System.Collections;
 
 
 
 public class CocoAndGlennAttacksIII : MonoBehaviour {
     
     Animator anim;
 
     public bool isAttacking = false;
     
     public Rigidbody fireball1;
     public Rigidbody iceball1;
     public Rigidbody fireball2;
     public Rigidbody iceball2;
     
     public float speed = 10;
     
     
     public bool attackVectorEast = false; 
     public bool attackVectorNorth = false;
     public bool attackVectorWest = false; 
     public bool attackVectorSouth = false;
     
 
     
     //public float triggerDownTime = 0;
     public float chargeDmg = 0;
     public float chargeTime = 2;
     
     public bool isCharging = false;
     
     Rigidbody clone;
     
 
     
     
     // Use this for initialization
     void Start () {
         anim = GetComponent<Animator>();
 
         
         
     }
     
 
     void Update () {
 
         
         float lastInputX = Input.GetAxis ("Horizontal");
         float lastInputZ = Input.GetAxis ("Vertical");
         
         if (lastInputX  > 0){ attackVectorEast = true;
             attackVectorWest = false; attackVectorNorth = false;attackVectorSouth = false;
         }
         else if (lastInputX < 0){ attackVectorWest = true;
             attackVectorEast = false; attackVectorNorth = false;attackVectorSouth = false;
         }
         else if (lastInputZ > 0) {attackVectorNorth = true;
             attackVectorWest = false; attackVectorEast = false;attackVectorSouth = false;
         }
         else if (lastInputZ < 0){ attackVectorSouth = true;
             attackVectorWest = false; attackVectorNorth = false;attackVectorEast = false;
         }
 
 
         
         if (Input.GetButtonDown("Fire1")){
             AttackCharger();
             isAttacking = true;
             anim.SetBool ("isAttacking", true);
             
             if(!isCharging){
                 isCharging = true;
 
 
 
              }
         }
 
         
              if (Input.GetButtonUp("Fire1"))
             {
 
 
                 if (attackVectorEast == true){
                     anim.SetBool ("isAttacking", true);
                     clone = Instantiate (fireball1, new Vector3(transform.position.x + 2.5f, transform.position.y, transform.position.z-0.5f), transform.rotation) as Rigidbody;
                     clone.velocity = transform.TransformDirection(Vector3.right * speed);
                 Physics.IgnoreCollision( clone.collider, transform.root.collider );
                     
                     Destroy (clone.gameObject, 2); 
                     clone = Instantiate (iceball1, new Vector3(transform.position.x+ 3f, transform.position.y, transform.position.z -0.5f), transform.rotation) as Rigidbody;
                     clone.velocity = transform.TransformDirection(Vector3.right * speed);
                 Physics.IgnoreCollision( clone.collider, transform.root.collider );
                     
                     Destroy (clone.gameObject, 2);
                 chargeDmg = 0;
                 }
                 if (attackVectorWest == true){
                     anim.SetBool ("isAttacking", true);
                     clone = Instantiate (fireball1, new Vector3(transform.position.x - 3f, transform.position.y, transform.position.z-0.5f), transform.rotation) as Rigidbody;
                     clone.velocity = transform.TransformDirection(Vector3.left * speed);
                 Physics.IgnoreCollision( clone.collider, transform.root.collider );
                     
                     Destroy (clone.gameObject, 2);
                     clone = Instantiate (iceball1, new Vector3(transform.position.x+ -2.5f, transform.position.y, transform.position.z -0.5f), transform.rotation) as Rigidbody;
                     clone.velocity = transform.TransformDirection(Vector3.left * speed);
                 Physics.IgnoreCollision( clone.collider, transform.root.collider );
                     
                     Destroy (clone.gameObject, 2); 
                 chargeDmg = 0;
                 
                 }
                 if (attackVectorNorth == true){
                     anim.SetBool ("isAttacking", true);
                     clone = Instantiate (fireball2, new Vector3(transform.position.x- 0.7f, transform.position.y, transform.position.z +1f), transform.rotation) as Rigidbody;
                     clone.velocity = transform.TransformDirection(Vector3.up * speed);
                 Physics.IgnoreCollision( clone.collider, transform.root.collider );
                     Destroy (clone.gameObject, 2);
                     clone = Instantiate (iceball2, new Vector3(transform.position.x+ 0.7f, transform.position.y, transform.position.z +1f), transform.rotation) as Rigidbody;
                     clone.velocity = transform.TransformDirection(Vector3.up * speed);
                 Physics.IgnoreCollision( clone.collider, transform.root.collider );
                     
                     Destroy (clone.gameObject, 2);
                 chargeDmg = 0;
                 }
                 if (attackVectorSouth == true){
                     anim.SetBool ("isAttacking", true);
                     clone = Instantiate (fireball2, new Vector3(transform.position.x- 0.7f, transform.position.y, transform.position.z -2.5f), transform.rotation) as Rigidbody;
                     clone.velocity = transform.TransformDirection(Vector3.down * speed);
                 Physics.IgnoreCollision( clone.collider, transform.root.collider );
                     Destroy (clone.gameObject, 2);
                     clone = Instantiate (iceball2,  new Vector3(transform.position.x+ 0.7f, transform.position.y, transform.position.z -2.5f), transform.rotation) as Rigidbody;
                     clone.velocity = transform.TransformDirection(Vector3.down * speed);
                 Physics.IgnoreCollision( clone.collider, transform.root.collider );
                     
                     Destroy (clone.gameObject, 2);
                 chargeDmg = 0;
                 
                 
                 
 
             }
         
             
 
             
             isAttacking = false;
             anim.SetBool ("isAttacking", false);
             
 
             isCharging = false;
             chargeDmg = 0;
 
         
         }
 
     }
 
  IEnumerator AttackCharger(){
         while(Input.GetButton ("Fire1")){
      if (chargeDmg < 10 ) { 
             chargeDmg += chargeTime*Time.deltaTime;
          yield return null;
             }
 
 }
     }
 
 
 }
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 Habitablaba · Oct 18, 2014 at 09:38 PM 0
Share

You have a lot of code here that isn't really relevant to your question, and it makes it difficult to see what is going on. Perhaps you can edit out some of the bits that aren't important to your specific question?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Habitablaba · Oct 18, 2014 at 09:38 PM

IEnumerator and yield return are meant to be used as co-routines, which must be started with StartCoroutine. Try using that instead of calling AttackCharge directly.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Yield Not Working - While Loop 3 Answers

Exiting an if statement when condition has been met 1 Answer

Nothing happening after WaitForSeconds C# 2 Answers

while loop not looping 2 Answers

Is this possible ? 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