Bug in Lecture: The Player Die() animation not work anymore

You see in this lesson at 9:58, Rick adds ProcessPlayerDeath(). And from then on, every time he tested the player dead, the player did not bounce back and just lay still. Animator dead sprite works, but his “myRigibody.velocity=deathKick” doesn’t work anymore.
When I applied this to my game, the same thing happened:
+playerRb.velocity = new Vector2(-playerRb.velocity.x, 10f);
+deathVFX.Play();
are not happening
So. Does anyone understand what happened?

The reason this happens is because calling the PlayerDeathSequence() method in GameSession is reloading the scene too quickly. Technically the two lines of code are still working, there just isn’t enough time to see it. (Even though those lines are written first, every line of code in Die() is executed in a single frame.) If you use Invoke to wait a few seconds before calling PlayerDeathSequence() it should behave as it did before.


void Die()
{
    if (playerCollider.IsTouchingLayers(LayerMask.GetMask("Enemy", "Hazards")))
    {
        isAlive = false;
        animator.SetTrigger("Death");
        playerRb.velocity = new Vector2(-playerRB.velocity.x, 10f);
        deathVFX.Play();
        Invoke("CallPlayerDeathSequence", 3f);
    }
}

void CallPlayerDeathSequence()
{
    FindObjectOfType<GameSession>().PlayerDeathSequence();
}

Be careful if you copy and paste this code exactly, since your code was shown as a screenshot rather than text I may have mistyped something. The important thing is to separate the final line of Die() into its own method so that you can call it with Invoke.

2 Likes

Nice.
Also, for safety use

Invoke(nameof(CallPlayerDeathSequence), 3f);
2 Likes

Oh, that a good way, thanks you very much

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms