// Start Pawn private static IEnumerator StartPawn(Respawner respawner, GameObject timerGO, float delay) { if (delay > 0f) { yield return(new WaitForSeconds(delay)); } respawner.RespawnStarted.Invoke(); respawner.gameObject.SetActive(false); float respawnTime = Random.Range(respawner.minRespawnTime, respawner.maxRespawnTime); for (float crTime = 0f; crTime < respawnTime; crTime += Time.deltaTime) { respawner.seconds = Mathf.RoundToInt(respawnTime - crTime); respawner.progress = crTime / respawnTime; yield return(null); } respawner.seconds = 0; respawner.progress = 1f; respawner.gameObject.SetActive(true); if (respawner.spawnMode == ESpawnMode.StartPosition) { respawner.m_Transform.position = respawner.nativePosition; respawner.m_Transform.rotation = respawner.nativeRotation; } // OnRespawn respawner.Send("OnRespawn") .RespawnEnded.Invoke(); if (respawner.smoothScale) { float smoothTime = .1f; Vector3 nativeScale = respawner.m_Transform.localScale; // while (smoothTime < 1f) { smoothTime += Time.smoothDeltaTime * respawner.scaleSpeed; respawner.m_Transform.localScale = nativeScale * smoothTime; yield return(null); } respawner.m_Transform.localScale = nativeScale; } // Remove timer if (respawner.spawnSFX == null) { Destroy(timerGO); } else { timerGO.transform.SetParent(Audio.PlayClipAtPoint(respawner.spawnSFX, respawner.m_Transform.position)); } }
// OnDie protected virtual void OnDie() { if (regeneration) { StopCoroutine("StartRegeneration"); } Vector3 nativePos = transform.position; nativePos.y += .15f; System.Action callSpawning = () => { foreach (GameObject deathObj in deathObjects) { if (deathObj != null) { (Instantiate(deathObj, nativePos, Random.rotation) as GameObject) .MoveToCache() .Send("SetOwner", lastDamage.owner) .Send("OnSpawn"); } } foreach (Pickup drop in deathDrops) { if (drop != null) { Vector3 randomPos = nativePos; randomPos.y += Random.value; randomPos.x += Random.Range(-1, 1f); randomPos.z += Random.Range(-1, 1f); drop.SpawnCopy(randomPos, Random.rotation) .MoveToCache() .Send("OnSpawn"); if (dropOnlyOnePickup) { break; } } } }; OnDead.Invoke(); if (spawnObjectsDelay > 0) { this.RunAction(callSpawning, spawnObjectsDelay); } else { callSpawning.Invoke(); } Respawner.StartRespawn(gameObject, destroyBodyDelay); }
// Destroy It private void DestroyIt(bool playSound = true) { if (playSound) { Utils.Audio.PlayClipAtPoint(pickupSound, m_Transform.position); } Respawner.StartRespawn(gameObject); }
// Start Respawn public static void StartRespawn(GameObject bodyObj, float delay = 0f) { Respawner respawner = bodyObj.GetComponent <Respawner>(); if (respawner == null) { Destroy(bodyObj, delay); return; } GameObject timerGO = new GameObject("respawTimerOf-" + bodyObj.name, typeof(EmptyMono)); timerGO.MoveToCache() .GetComponent <MonoBehaviour>() .StartCoroutine(StartPawn(respawner, timerGO, delay)); }