Jump to content
  • entries
    44
  • comments
    19
  • views
    2320

Day 22 of 100 Days of VR: Creating the Game End State and Game Over Animation

Josh Chang

683 views

Hi and welcome back to day 22 of the 100 days of VR!

Yesterday, we created our game over panel to show to the player when they lose, however in the current state of the game, the game over panel is being displayed at the start of the game.

Today, we’re going to:

  1. Create a new state controller for our animation so that we don’t instantly play our animation
  2. Play the animation when the player is defeated and all states related to that
  3. Restart the game when the player selects to play again

There’s a lot ahead of us today, so let’s get started!

Creating an Animation State Controller

Right now, our animation just plays whenever the game starts.

The reason for that is, because of our animator controller.

Go to our GameOver.controller in the Animation folder and double click it to open it up in our Animator tab.

 
0*z0i_mO5cF64pjudU.png

Right now, Game Over is the default state we transition into from Entry.

As a result, we always play the Game Over clip that’s attached to it be default when we start our game.

To fix this, we’re going to do a couple of things. We’re going to:

  1. create a new default state,called Start that is our default transition from Entry that transitions into our Game Over state
  2. create a new boolean parameter IsGameOver
  3. set a transition from Start to Game Over
  4. select the transition arrow from Start to Game Over and create a new condition: IsGameOver = true

After you’re done, you should have something like this:

 
0*5FqEr7ymDA25kT3i.png

Now if we’re to play our game again, nothing will show up!

Playing the Game Over animation

Now that we have the correct game states set up, it’s time to use it.

All we need to do is play the animation when our player’s health drops to 0.

Here are the changes we’re going to do:

  1. Create a new GameManager script that will take care of the logic of the ending
  2. Change our PlayerHealth script to use the GameManager to tell it when the game is over.

The first thing we’re going to do is create an empty game object that we’ll call GameManager.

Then create a new script called GameManager and attach it to our new game object.

Here’s what our GameManager script will look like:

 
using UnityEngine;
public class GameManager : MonoBehaviour
{
    public Animator GameOverAnimator;
    public void GameOver()
    {
        GameOverAnimator.SetBool("IsGameOver", true);
    }
}
 

The code right now is straightforward:

  1. We get the GameOverAnimator from our Game Over object and we take the Animator from it.
  2. We create a public GameOver() that someone will call that will set our IsGameOver parameter to be true so that we’ll play the animation to show our game over panel.

When you’re done with the script, make sure to set our GameOver UI GameObject to our GameOverAnimator slot in our GameManagercomponent.

Quick note on pushing vs pulling for updates

For those of you who saw Unity’s tutorial, I’m not actually a big fan of what Unity does with having all of these checks in the Update(), where we’re always checking for something to happen.

Personally, I much prefer to take a more push functionality where we only do something, if something else notifies us of it happening.

A perfect example of a push approach is our public GameOver(). Currently we have some other script call the function to indicated game over.

An example of pulling is that we have a script that checks for changes every frame. For example. We could have given GameManager a reference the the player Game Object and in Update() it could constantly check to see if the player is dead.

Both way works, I just prefer the way where we only do something when we’re told to do it and not check every frame.

Using our GameManager

After creating our GameManger, we need to use it.

In this particular case, we’re going to use it in our PlayerHealth script. We’re going to:

  1. Add our GameManager object
  2. Call GameOver() when the player’s health is 0 or below

Here’s our code:

 
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour
{
    public Slider HealthBar;
    public float Health = 100;
    private float _currentHealth;
    private GameManager _gameManager;
    void Start ()
    {
        _currentHealth = Health;
        _gameManager = Object.FindObjectOfType<GameManager>();
    }
    public void TakeDamage(float damage)
    {
        _currentHealth -= damage;
        HealthBar.value = _currentHealth;
        if (_currentHealth <= 0)
        {
            _gameManager.GameOver();
        } 
    }
}
 

The code here is also straightforward:

  1. We create a private field for our GameManager class
  2. In Start() we instantiate our GameManager
  3. In TakeDamage(), after our player takes damage and health falls to 0, we would call GameOver() which will show the Game Over panel.

Restarting the Game

Now that we have our game over panel showing up, it’s time for us to disable some functionality so that we can click on the restart button!

There’s a couple of things we need to:

  1. in GameManager, when GameOver() is called disable the player’s movements and re-enable our cursor to select a button
  2. create a GameOverUIManager that will deal with the logic of clicking a button
  3. change our GameOver game object’s Canvas Group to ignore parent elements and check the Blocks Raycasts option so we can click on the button

Updating GameManager to Disable the Player in Game Over

When the game is over, we want to:

  • disable all the scripts that are involved with the controls of the player. It would be weird that at game over we can still move around and shoot.
  • re-enable our Cursor which we locked in the center of the game

Here are the changes we did to GameManager:

 
using UnityEngine;
public class GameManager : MonoBehaviour
{
    public Animator GameOverAnimator;
    private GameObject _player;
    void Start()
    {
        _player = GameObject.FindGameObjectWithTag("Player");
    }
    public void GameOver()
    {
        GameOverAnimator.SetBool("IsGameOver", true);
        _player.GetComponent<PlayerController>().enabled = false;
        _player.GetComponentInChildren<MouseCameraContoller>().enabled = false;
        _player.GetComponentInChildren<PlayerShootingController>().enabled = false;
        Cursor.lockState = CursorLockMode.None;
    }
}
 

As you can see, we:

  1. Created a reference to our player character in Start()
  2. In GameOver() we disabled the player movement script, our mouse script, and our shooting script. We also re-enabled our lock

With this, if you were to play the game right now and let our player die, we regain our mouse and we won’t be able to control our player anymore.

 
0*Py9582hVtm-nCfza.png

Creating the Code for our UI Button Click

Next, we need some code to take care of what happens when we click on our button.

To do that we need to create a onClick event listener for our button.

For those of use that aren’t familiar with the concept of event listeners, you can think of it as code that will only execute when you take a certain action.

In this specific instance, our Button has a onClick event listener so it’ll wait for whenever the user clicks on the button.

When someone does click the button, they’ll execute any function that was passed to it. In this case, our restart code.

Let’s get started.

First, create a new script for our GameOver game object in the hierarchy. We’re going to call it GameOverUIManager. We’re going to use it to manage our play the game when the user clicks play again.

Here’s what it looks like:

 
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameOverUIManager : MonoBehaviour
{
    private Button _button;
    void Start () {
        _button = GetComponentInChildren<Button>();
        _button.onClick.AddListener(ClickPlayAgain);
    }
    public void ClickPlayAgain()
    {
        SceneManager.LoadScene("Main");
    } 
}
 

Now let’s walk through the code:

  1. We have a private _button that is the button from our Game Over Panel.
  2. In Start() we instantiate our button and we set the OnClick Listener to our button to run ClickPlayAgain() whenever the user clicks the button
  3. In ClickPlayAgain(), we use the SceneManager to restart our scene so we can play again. As we might recall, everything in Unity runs in a scene, SceneManager is what we use to help us transition from one scene to the next. Note: using LoadLevel is depreciated for SceneManager

Now with all of this, we’re almost done!

If we were to try and play the game now and let the knight beat us, you’ll realize a problem. We can’t click on the Restart button!

Let’s fix it.

Allow Clicking on the Button UI

The main reason why we can’t touch our button UI is because of our Group Canvas component attached to HUD. We disabled everything so that our UI will just be something the user sees.

 
0*0sBQ8tNLSRLJBNEP.png

We could enable Interactable and Block Raycasts so that our Graphics Raycaster script will be able to detect the button being clicked, like explained here.

However, if we were to do that, the problem is that everything else in the game would become interactable, including our health bar!

We don’t want that, so instead we’re going to make a separate Canvas Groupcomponent just for GameOver.

We need to do 2 things:

  1. Add a new Group Canvas component and check all settings so that we can interact with our game over panel and ignore the rules set by the parents.
  2. Add a Graphic Raycaster, which is used to detect if the user clicks on anything in the canvas.

Once we have these setup, play the game again and you’ll see that we can restart the game!

 
0*4PyQx-NcAjyQglw5.png

Conclusion

We did a lot of work today!

We:

  • Created a new animation state for our GameOver animations
  • Enabled our animation when our player is defeated
  • Allowed the user to restart the game when they lose

There are some more things that we can add to our Game Over scene to make things more complete, however, tomorrow, we’ll jump into a new topic, specifically enemy spawning!

Until then!

Source: Day 22

Visit the 100 Days of Unity VR Development main page.

Visit our Homepage



0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
  • Advertisement
  • Blog Entries

  • Similar Content

    • By Rio Lloyd
      Hey all!
      we are a team of 3 looking for more members, 
      we are making an isometrical Survival RPG.
      we are looking For Members who can make low poly 3D artists who can do character models, environments, tools and more.
       
      if interested and want to know more email me at rioishere14@gmail.com
    • By nxrighthere
      BenchmarkNet is a console application for testing the reliable UDP networking solutions.
      Features:
      Asynchronous simulation of a large number of clients Stable under high loads Simple and flexible simulation setup Detailed session information Multi-process instances Supported networking libraries:
      ENet UNet LiteNetLib Lidgren MiniUDP Hazel Photon Neutrino DarkRift More information and source code on GitHub.
      You can find the latest benchmark results on the wiki page.
       
    • By trapazza
      I'm trying to add some details like grass, rocks, trees, etc. to my little procedurally-generated planet. The meshes for the terrain are created from a spherified cube which is split in chunks (chunked LOD).
      To do this I've wrote a geometry shader that takes a mesh as input and uses its vertex positions as locations where the patches of grass will be placed (as textured quads).
      For an infinite flat world (not spherical) I'd use the terrain mesh as input to the geometry shader, but I've found that this won't work well on a sphere, since the vertex density is not homogeneous across the surface.
      So the main question would be: How to create a point cloud for each terrain chunk whose points were equally distributed across the chunk?
      Note: I've seen some examples where these points are calculated from intersecting a massive rain of totally random perpendicular rays from above... but I found this solution overkill, to say the least.
      Another related question would be: Is there something better/faster than the geometry shader approach, maybe using compute shaders and instancing?
    • By FedGuard
      Hello all,
       
      I would like to start off with thanking you all for this community. Without fora like these to assist people the already hard journey to making an own game would be exponentially more difficult. Next I would like to apologize for the long post, in advance...
      I am contemplating making a game. There, now that's out of the way, maybe some further details might be handy.
      I am not some youngster (no offence) with dreams of breaking into the industry, I am 38, have a full-time job, a wife, kid and dog so I think I am not even considered indie? However I recently found myself with additional time on my hands and decided I would try my hand at making a game.Why? Well mostly because I would like to contribute something, also because I think I have a project worth making (and of course some extra income wouldn't hurt either to be honest). The first thing I realized was, I have absolutely no relevant skill or experience. Hmm; ok, never mind, we can overcome that, right?
      I have spent a few months "researching",meaning looking at YouTube channels, reading articles and fora. Needless to say, I am more confused now than when I started. I also bought some courses (Blender, Unity, C#) and set out to make my ideas more concrete.
      I quickly discovered, I am definitely not an artist... So I decided, though I do plan to continue learning the art side eventually, I would focus on the design and development phase first. The idea being, if it takes me a year or more solely learning stuff and taking courses without actually working on my game, I would become demoralized and the risk of quitting would increase.
      So I thought I would:
      1: Keep following the courses Unity and C# while starting on the actual game development as the courses and my knowledge progress.
      2: Acquire some artwork to help me get a connection with the game and main character, and have something to helm keep me motivated. (I already did some contacting and realized this will not be cheap...). Also try to have the main character model so I can use it to start testing the initial character and game mechanics. For this I have my first concrete question. I already learned that outsourcing this will easily run up in the high hundreds or thousands of dollars... (lowest offer so far being 220 USD) I am therefore playing with the idea of purchasing https://assetstore.unity.com/packages/3d/animations/medieval-animations-mega-pack-12141 with the intention of then have an artist alter and/or add to the animations (it is for a Roman character so some shield animations are not going to work the same way.). This way I could start  with the basic character mechanics. Is this a good idea, waste of money,...? Any suggestions? I then have a related but separate question. Is it a good idea to buy Playmaker (or some other similar software I haven't yet heard of like RPGAIO), and using this for initial build, then changing/adding code as the need arises?
      3.Get a playable initial level ready as a rough demo and then starting to look for artist for level design and character/prop creation.
      ...
       
      I would really appreciate some input from more experienced people, and especially answers to my questions. Of course any advice is extremely welcome.
    • By GameTop
      Dirt Bike Extreme - another game made with Unity. Took about 2 months to complete.
      Take part in extreme motorcycle races across the dangerous and challenging tracks. Dirt Bike Extreme is easy to pick up but hard to master. Race, jump and crash your way and other mad rivals through the amazing tracks as you master the skills and physics of motocross in this high-speed racing adventure. Conquer challenging routes on 23 different runs, discover new bikes and become the best of the best! Over 257K downloads already!
      Windows Version:
      https://www.gametop.com/download-free-games/dirt-bike-extreme/

      Mac Version:
      https://www.macstop.com/games/dirt-bike-extreme/
       

       


×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!