Acem Gaming
  • Home
  • Tutorials
    • Unity Beginner Game Design >
      • Game Design Intro
      • Unity Environment
      • Intro to Visuals
      • Intro to Coding
      • Intro to UI Design
      • Intro to Physics
      • Intro to Materials, Textures, and Lighting
    • Unity C# Tutorials >
      • Beginner >
        • Introduction
        • Variables
        • Operators
        • Conditional Statements
        • Loops
        • Methods
        • Arrays
        • Structures
        • Classes
        • Enumerations
      • Intermediate >
        • Type Conversion
        • Switches
        • Inheritance
        • Encapsulation
        • Polymorphism
        • Static
        • Properties
        • Interfaces
        • Lists
        • Dictionaries
      • Advanced >
        • Delegates
        • Events
        • File IO
        • Stacks
      • DesignPatterns >
        • Proxy
        • BehavioralPatterns >
          • ChainOfResponsibility
    • Unity Oculus Rift Tutorials >
      • Set-Up
      • Camera Set-up
      • Object Interaction
    • Unity Odds & Ends >
      • Unity UI Tutorial : Healthbar Part 1
      • Unity UI Tutorial: Healthbar Part 2
      • Unity Scriptable Object Tutorial
      • Vector Math
      • Lerp
      • Basics of Motion Physics
      • Unity Physics Basics
      • Clamping Values
      • Linear Physics Movement
    • Legacy Tutorials
  • Game Demos
    • WebGLTest
  • Blog

22 - Events
In this tutorial we will learn how events build upon the functionality of delegates that we learned about in the last tutorial.  Events are actions such as user-clicks, keyboard input, and internal message systems.  Events follow a publisher-subscriber model.  A publisher-subscriber model is a framework which allows entities called subscribers the ability to subscribe to events created by an entity called the publisher.  The goal of events is to perform a set of actions when some event happens.  Multiple actions may happen when an event happens, and can dynamically change as the game progresses.  In this tutorial we will set-up a list of events to happen when our player falls of the map.  For this tutorial we will set our scene up like shown below.  You can re-use the simple player movement found in previous tutorials to move the character.  Also, create a script called EventManager which will be attached to the DeathArea GameObject and a EventTutorial script which will be attached to our player.
Picture
Creating Event Manager Class
The first step to using events is defining a delegate.  This delegate is going to shape how all of the methods that are being called by our event are shaped.  If you want different types of methods to be called by an event, you can create multiple delegates within the EventManager class.  For this tutorial we would like to reset our player's position and subtract a life when our player falls off the map.  We can do both of these with void functions if we attach the method script to the player.  This means that we will only require one delegate for the event.  I labeled mine DeathDel.
Picture
The next step is to create the event.  This will be done in the event manager class and we will make it static so that it can be called anywhere.  We'll actually create two events for this tutorial.  One will be used to subtract the player's lives and reset their position.  The other will be used to display game over text once the player has run out of lives.
Picture
The final step for creating the event is creating the code that will be the calling factor on the event.  In this tutorial, our event is going to occur when the player falls off the map, and when the player has no more lives respectively.  We accomplish this by utilizing an invisible gameobject with a box collider attached to it.  This box collider will be used to detect a trigger from the player colliding with it.  Upon colliding, we would like to reset the player's position and subtract a life. We would also like to call the GameOverEvent when the players current lives are at zero.  We are able to accomplish this with the same trigger being when the player collides with the death area.
Picture
Using the Event
Now that we have our event created, we need to actually use the event.  The first step to utilizing events is to have methods that the event will call when it occurs.  We will create two seperate methods for this tutorial.  A void method which changes the position of the player back to 0,0,0.  We will also create another void method which subtracts a life from the player.  We will also create a method to display the gameover text. You'll see we unsubscribe to the resetposition once we have 1 life left and then we unsubscribe from the subtract life event when we have 0 lives.  This will be more clear in the next portion.
Picture
Picture
The next step to utilizing events in Unity is to subscribe to the event.  Generally, this is accomplished through the enable and disable methods within the class that the events methods are in.  We use the "+=" operator to subscribe to events and teh "-=" operator to unsubscribe to events.  We put them in the OnEnable and OnDisable methods.  Notice the format of how we subscribe to events.  We are subscribing to the specific event that we defined in the Event Manager class.  This links the ResetPosition, SubtractLives, and OnGameover methods to the event of falling off the map.  
Picture
 Make sure that you have all of the fields assigned in the inspector and then try out the script by jumping off the cliff three times.  You should get a result similar to what is below.  Notice that by unsubscribing to the ResetPosition event we don't reset our position after the last life is gone.
Picture
Conclusion
In this tutorial we learned how to create a simple event system where a player loses a life and reset's their position when they fall off the map.  This simple example should explain the process of using events in Unity and you should be able to extend this concept to your own code.  What is the benefit of using events?  We can dynamically change how this event behaviour acts at runtime.  If we create a level where the player might fall of the map, but we would not want them to lose a life, we can unsubscribe the SubtractLives method and only call the ResetPosition method when the player falls off the map.  It is this real time behaviour that makes events so powerful.  It allows us to customize our game flow as we go.  
Video

Services

Service One
Service Two
Service Three

Company

About
The Company
Menu

Support

Contact
FAQ
Terms of Use
© COPYRIGHT 2015. ALL RIGHTS RESERVED.
  • Home
  • Tutorials
    • Unity Beginner Game Design >
      • Game Design Intro
      • Unity Environment
      • Intro to Visuals
      • Intro to Coding
      • Intro to UI Design
      • Intro to Physics
      • Intro to Materials, Textures, and Lighting
    • Unity C# Tutorials >
      • Beginner >
        • Introduction
        • Variables
        • Operators
        • Conditional Statements
        • Loops
        • Methods
        • Arrays
        • Structures
        • Classes
        • Enumerations
      • Intermediate >
        • Type Conversion
        • Switches
        • Inheritance
        • Encapsulation
        • Polymorphism
        • Static
        • Properties
        • Interfaces
        • Lists
        • Dictionaries
      • Advanced >
        • Delegates
        • Events
        • File IO
        • Stacks
      • DesignPatterns >
        • Proxy
        • BehavioralPatterns >
          • ChainOfResponsibility
    • Unity Oculus Rift Tutorials >
      • Set-Up
      • Camera Set-up
      • Object Interaction
    • Unity Odds & Ends >
      • Unity UI Tutorial : Healthbar Part 1
      • Unity UI Tutorial: Healthbar Part 2
      • Unity Scriptable Object Tutorial
      • Vector Math
      • Lerp
      • Basics of Motion Physics
      • Unity Physics Basics
      • Clamping Values
      • Linear Physics Movement
    • Legacy Tutorials
  • Game Demos
    • WebGLTest
  • Blog