Saturday 28 July 2012

Death, Timings and Out-Of-Bounds

A busy night!


I have finished implementing a nice little state system for the level. There's a placeholder pre-level state, the main game state and a results state.

The state system inspects a game monitor object, which is in turn notified of important events, such as when the helicopter touches a deadly red object. This is a nice way of stopping things messing with things they should know nothing about. The helicopter shouldn't know about the state system, as once it does, it could mess with it. All the helicopter can do is tell the game monitor that it's hit an object. No danger there!

I've also implemented a neat little out-of-bounds checker. I added an empty game object and added a collision box to it. I marked it as a trigger so that Mike wouldn't physically collide with it, and resized it to fit the whole level, plus a bit of padding. I then added this script to the object:


using UnityEngine;
using System.Collections;


public class OutOfBoundsScript : MonoBehaviour
{
GameObject m_heliGO = null;
GameMonitorScript m_gameMonitorScript;


void Start ()
{
m_heliGO = GameObject.Find("HeliCube");
    m_gameMonitorScript = GameObject.Find("GameMonitor").GetComponent("GameMonitorScript") as GameMonitorScript;
}

void OnTriggerExit( Collider other )
{
if( other.gameObject == m_heliGO )
{
// out of bounds!
m_gameMonitorScript.notifyOutOfBounds();
        }
    }
}



All it does is cache the helicopter and game monitor on start, then when the heli exits the trigger it notifies the game monitor. Simple!

The state machine then asks the game monitor whether out of bounds has been detected and it kills the game. Nice one!

No comments:

Post a Comment