- Home /
Question by
sepharg · Oct 03, 2016 at 05:59 AM ·
mecanimanimations
Death animation and respawn?
I´m trying to make a 2d character to die when colliding with an enemy, then play a death animation and after that, respawn at a specified location. It doesn´t seem to be working, what I did is:
create a "Death" animation. created a "Dying" trigger. From Any State I can transition to "Death" based on the "Dying trigger (i also tried this with bools but to no avail) Then I have the following code that gets called when the player collides with an enemy (RespawnPlayer)
public void RespawnPlayer() { StartCoroutine("PlayerDieCo"); } public IEnumerator PlayerDieCo() { var rigidBody = player.GetComponent<Rigidbody2D>(); var animator = player.GetComponent<Animator>(); rigidBody.velocity = Vector2.zero; animator.SetTrigger("Dying"); var seconds = GetAnimationClip(animator, "RobotDeath").length; yield return new WaitForSeconds(seconds); yield return StartCoroutine("RespawnPlayerCo"); } public IEnumerator RespawnPlayerCo() { var rigidBody = player.GetComponent<Rigidbody2D>(); var script = player.GetComponent<Platformer2DUserControl>(); var animator = player.GetComponent<Animator>(); rigidBody.velocity = Vector2.zero; script.enabled = false; player.GetComponent<Renderer>().enabled = false; yield return new WaitForSeconds(respawnDelay); player.transform.position = currentCheckpoint.transform.position; script.enabled = true; player.GetComponent<Renderer>().enabled = true; }
The problem is that when the collision happens the player dissapears first, then I cannot move it, then it respawns and then dies. I know it´s something related to the order of execution but can´t seem to fix it, any suggestions?
Comment