Scripting the player character's respawningÂ
In the previous section, we modified the CucumberManManager
script to track how many lives remain and to destroy the UI image elements as appropriate. In this section, we will modify that script to accomplish the following when a life is lost:
- Play the die animation
- Respawn the player on a respawn pad
Let's start by modifying the CucumberManManager
script:
- Add the following class variables:
publicTransformSpawnPad1; publicTransformSpawnPad2; publicTransformSpawnPad3;
- In the
Hierarchy
panel, select theCucumberMan
 GameObject and, in theInspector
panel, scroll until you locate theCucumber Man Manager (Script)
component. - Drag
SpawnPad1
,SpawnPad2
, andSpawnPad3
from theHierarchy
view to the designated spots in theInspector
panel. See the following for details:
- Modify the bottom section of the
Update()
method, as shown here:
if(livesRemaining==2){ Destroy(GameObject.Find("Life3")); anim=GetComponent<Animator>(); anim.Play("CM_Die"); StartCoroutine...