Dear Community,
In my game I come to a point where I want several tasks to be performed before the player can interact with the game again. The tasks being: My 5 heroes attacking all 3 enemies one after another by shooting balls at them. Then the enemies attacking the heroes one after another and finally instantiating a new bunch of objects into a grid. I am using coroutines to do that. The problem is that at this point my computer is going wild. (loud and hot - its a macbook 2015) And I don’t know why. Since I’d like to eventually play this game on my iPad, it really needs to be simple.
Here is a picture:
Essentially the code goes like this:
/*
using ...
public class InputManager : MonoBehaviour {
public Hero[] heroes;
public Monster[] monsters;
public GridManager grid;
public int timesToClick = 4;
void Update () {
// When you click on the grid, deactivate the tiles beneath.
// timesToClick--;
// Instantiate a gameObject "Ammo".
// start Ammo's coroutine that lets it fly to the hero.
// in Ammo's script: when "Ammo"'s.position == hero.position call hero.ReceivedAmmo
// that starts the coroutine Fight() in this script
}
IEnumerator Fight() {
foreach (Hero hero in heroes) {
hero.Attack();
yield return new WaitForSeconds (timeBetweenAttacks);
}
foreach (Monster monster in monsters) {
monster.Attack();
yield return new WaitForSeconds (timeBetweenAttacks);
}
grid.UpdateGrid();
yield return new WaitForSeconds (timeToUpgradeGrid);
timesToClick = 4;
}
}
*/
hero.Attack and monster.Attack are functions in my hero and monster class. When my heroes attack they each instantiate 3 “Ball” objects (another custom class), and start “Ball’s” coroutine moving their transform from the heroes position to each of the monsters position. Essentially like this:
/*
public class Ball
public static int ballCount;
public float speed;
private Vector3 targetPos;
void Awake {
ballCount++;
}
IEnumerator Attack(Monster target, int damage)
{
targetPos = target.transform.position;
while (this) {
speed *= Time.DeltaTime;
transform.position = Vector3.MoveTowards (transform.position, targetPos, speed);
if (transform.position == targetPos) {
target.HasBeenDamaged(damage);
ballCount--;
Destroy (this.gameObject);
}
yield return new WaitForFixedUpdate ();
}
}
*/
This essentially is the Hero Script:
/*
public class Hero : MonoBehaviour {
public int damage;
public InputManager InputManager;
void ReceivedAmmo () {
damage++;
//do some UI stuff
if (inputManager.timesToClick == 0) {
StartCoroutine (inputManager.Fight());
}
}
}
*/
Can anyone explain why this is so computationally demanding?