This page goes over the game cycle and respawn mechanisms.

How does respawn work?

In most platform games, your character can take damage, and eventually die if its health reaches zero. In the Corgi Engine that’s also the default behaviour. When your character takes damage, a relatively complex chain of events starts :

  • your character takes X damage, this is handled by the Health component of your character
  • it loses X health
  • if its health goes below zero, the Health component calls its Kill() method and tells the LevelManager that the player is dead
  • the LevelManager resets the level, triggering the Respawn of the player and potentially other objects within the scene

That Respawn is done by checking what checkpoint the player last reached, repositioning the player there, and also respawning all the objects registered to that checkpoint. Objects register automatically to checkpoints in the scene. If you look at any LevelManager in any of the demo scenes for example, you’ll see a “Checkpoint Attribution Axis” checkbox in its inspector. By default it’s set on the X axis. Let’s say you have a horizontal level (think Super Mario), and three checkpoints on it. Every enemy or object in that level with an Autorespawn component (or with a component that implements the Respawnable interface) will, at the start of the level, register to the first checkpoint to its right. Which means that if your character dies between checkpoints B and C, all enemies it may have killed between them will respawn too.

If you want your enemies or objects to respawn, just put an Autorespawn component on them. This component will let you determine how exactly the object should respawn, as well as how many times it should do so. You can also condition respawn to certain checkpoints, or just opt to have checkpoints ignored, in which case the object will always respawn.

Setting up an enemy so that it respawns after the Player character dies

  • in a new project in Unity 2019.4.26f1, import Corgi Engine v7.1.
  • open the MinimalLevel demo scene
  • drag a Bear3D prefab into the scene, position it at -11,-4,0
  • drag a Urchin prefab into the scene, position it at 5,-2,0
  • select the Bear3D, add a AutoRespawn component to it
  • press play, jump on the bear to kill it, then move right and jump on the urchin to die
  • after the respawn delay, the player respawns, and so does the bear

Checkpoints

Jekyll
Checkpoints being linked by a green line in scene view while game is running.

Creating and placing checkpoints in the Corgi Engine is extremely simple. All you have to do is create an empty game object, and add a Checkpoint component to it. It should automatically add a BoxCollider2D to it. All you have to do then is change the BoxCollider2D’s size, and set isTrigger:true. That size will determine the zone that will trigger the checkpoint, make it big enough so that your character won’t miss it. You can also set the facing direction from the Checkpoint component’s inspector. That’s the direction your player character will be facing when respawing.

You can then position your checkpoint in your scene, and add a few more. One last thing you can do is go to your LevelManager’s inspector, and define the Checkpoint Attribution Axis. If your level is horizontal, go for X, if it’s vertical for Y. If your levels are non linear, you can also opt to have your checkpoints’ order defined by each checkpoint’s CheckPointOrder value. When you press play, you’ll see all your checkpoints linked by a green line in the order they’ll have to be crossed.

Checkpoints also have an option in their inspector to Force Assignation. If set to true, that checkpoint, when met, will automatically become the last active checkpoint.

Adding a checkpoint to a scene

  • fresh install of Corgi Engine v7.5.1 on Unity 2019.4.34f1
  • open the RetroAI demo scene
  • create a new empty GameObject, name it MyCheckpoint, position it at -63,-5,0
  • add a BoxCollider2D to it, set its size to 1,10, isTrigger:true, and add a Checkpoint component to it
  • press play, walk right under the red platform, go through the checkpoint and die on the spikes, you’ll respawn at the checkpoint