Sounds like you want to do the following…
1, Detect when an enemy hits the player
2, Create a gameover visual that can be presented when the player is hit
3, Play gameover animation
4, Restart the game when the animation is finished
re 1
To detect when an enemy hits the player you have a number of options. I would suggest adding BoxCollider or 2DBoxCollider component to both your player and enemy(ies) gameobject and making them triggers, by checking the “Trigger” checkbox in the component properties – this avoids weird things happening with the physics engine e.g. collisions getting resolved in ways you don’t want, like your player getting tossed way in the air.
Edit the colliders using the input fields or by pressing the Edit Collider button so they enclose your player and any enemy.
You’ll also want to add a RigidBody to these gameobjects to ensure collisions register. Unless you’re using Physics to move your player and enemy, you’ll want to set the RigidBody to IsKinematic (which means your code, not the physics engine will move the object).
To actually get notified about a collision you’ll need to write some code. For now, lets keep things simple by making one new script: PlayerCollisionController.cs. You’ll want to add a single event listener in that script…
using UnityEngine;
using System.Collections;
public class PlayerCollisionController : MonoBehaviour {
void OnTriggerEnter(Collider other) {
// todo : player is dead, show game over
}
... snipped Start(), Update(), etc...
}
Add that script to the Player gameobject.
re 2
Make a gameover visual, lets for now say it’s a sprite, add it to the scene.
Open the animation tab, and create a new animation for the gameover visual, perhaps a scale effect. Because this is the first animation on the gameobject it will be set to play as the default.
Select the animation, in the project explorer and turn off looping, unless you want it to loop :).
Disable the gameover gameobject, by unchecking the checkbox at the top left of the gameobject’s inspector.
Now, you need to associate the gameover gameobject with the code that handles a collision. You can do this by exposing a parameter from the Collision script and dragging the gameover object into the slot it exposes in the inspector. So let’s go back and edit the Collision script…
using UnityEngine;
using System.Collections;
public class PlayerCollisionController : MonoBehaviour {
[SerializeField]
private GameObject _gameover;
void OnTriggerEnter(Collider other) {
// todo : player is dead, show game over
_gameover.SetActive(true);
}
... snipped Start(), Update(), etc...
}
re 3
By activating the gameover gameobject the animation you made should start playing.
You’ll probably want to note that the game is now in a “gameover” state and stop allowing for player input or play some kind of death animation. I’ll let you dig into that.
re 4
To restart the level you can use a single line of code in response to an animation event. I would create a new script called GameOverManager.cs and expose a public event GameOverEnded(). This event can be added to the animation timeline, and fire when you want. Read up on Animation Events. Inside the GameOverEnded event you can use a single line of code…
using UnityEngine;
using System.Collections;
public class GameOverManager : MonoBehaviour {
public void GameOverEnded()
{
Application.LoadLevel (Application.loadedLevelName);
}
}
If you wanted to record the player’s score or give them a reward or something you’ll want other code to execute on GameOverEnded(). Read up on PlayerPrefs (to save state).
Okay. Hope that helps. Cheers!