void OnTriggerEnter2D(Collider2D other) { Client_controller newClient = other.GetComponent <Client_controller>(); if (newClient != null && !angryClients.Contains(newClient)) { angryClients.RemoveAll(item => item == null); //In case clients have been destroyed before event start, remove them if (newClient.status != "event") //Make sure to set right status { newClient.assignToEvent(gameObject.transform.position); } angryClients.Add(newClient); // Debug.Log("New fighting client. Current nb : "+angryClients.Count); if (angryClients.Count > 1) //Start fight { foreach (Client_controller client in angryClients) //Turn off client (to merge for a fight) { client.gameObject.SetActive(false); } animator.SetBool("active fight", true); //Block movement Obstacle.enabled = true; ObsCollider.isTrigger = false; //Trigger becoming solid lifeTimer = lifeTime; //Time before end of the fight } } }
//InvokeRepeating() is another option //Coroutine to be started in a parallel process. private IEnumerator clientCoroutine(GameObject clientObj) { Client_controller client = clientObj.GetComponent <Client_controller>(); while (EventManager.Instance != null) { if (GameSystem.Instance.serviceOpen) { //Try to spawn softObs or hardObs randomly if (Random.value < 0.5f) { if (client.status == "consuming") //Only spawn soft obs while consuming { EventManager.Instance.spawnSoftObs(clientObj.transform.position, spawnChanceSoft); } else { List <GameObject> otherClients = findNearClients(clientObj, 1.0f); if (otherClients.Count > 0) { foreach (GameObject ocl in otherClients) { Debug.DrawLine(clientObj.transform.position, ocl.transform.position, Color.red, coroutineRefreshRate); } // Debug.Log("Clients near"); GameObject tgtClient = otherClients[Random.Range(0, otherClients.Count)]; //TODO : Compute spawnChance w/ clients happiness Vector2 eventPos = (clientObj.transform.position + tgtClient.transform.position) / 2; //Event pos between clients List <Client_controller> targetClients = new List <Client_controller>() { client, tgtClient.GetComponent <Client_controller>() }; EventManager.Instance.spawnHardObs(targetClients, eventPos, spawnChanceHard); } } } } yield return(new WaitForSeconds(coroutineRefreshRate)); //Called every coroutineRefreshRate second } }