- Home /
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;
}
}
}
}
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?
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.
Your answer
Follow this Question
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