I’m creating a side-scrolling children’s game that teaches them the importance of conserving and recycling water.
In the game, there is a character called Chelsea who needs to collect 1000 milliliters of rain water to use in and around her home for later use, by simply running to and standing inside of an invisible box-collider - a spot in the game where some rain falls.
Chelsea will then need to run back and forth from this spot, emptying her bucket that she is using to collect the rain water, to a watering tank located on the other side of the playing field.
The obstacle of the game is to avoid another character by simply jumping over them or the amount of water she has collected in her bucket will be reset back to 0.
Everything is working fine, however when the pause is activated, the amount of water she has in her bucket will continue to add-up/collect, while she is inside of the box-collider.
The ‘Pause’ Script:
var skin:GUISkin;
var allowPause:boolean = true; // allow ESC to bring up pause menu
var startPaused:boolean = false; // start game with pause menu up
var legalwidth:int = 120;
var menutop:int=80;
private var gldepth = -0.5;
private var startTime = 0.1;
var mat:Material;
private var tris = 0;
private var verts = 0;
private var savedTimeScale:float;
private var showfps:boolean;
private var showtris:boolean;
private var showvtx:boolean;
private var showfpsgraph:boolean;
var checklegal:boolean = false;
var publisher = "mywebsite.com.";
var srcValue = null;
var lowFPSColor = Color.red;
var highFPSColor = Color.green;
var lowFPS = 30;
var highFPS = 50;
var start:GameObject;
var goal:String = "Save as much water as you can!"; // goal statement displayed on pause menu
var statColor:Color = Color.yellow;
var hudColor:Color = Color.white;
// fill in the help text that is initially presented
var help:String[] = ["ESC or P - Pause Game"];
// fill in the credit info for your game
var credits:String[]=[
"© 2014 All Rights Reserved.",
"http://mywebsite.com/"] ;
var crediticons:Texture[]; // images you want shown on the credit screen
// enums for each page of the menu (you can think of each as a state in a state machine)
enum Page {
None,Main,Options,Credits,Help
}
private var currentPage:Page;
// fps graph
private var fpsarray:int[];
private var fps:float;
function Awake() {
fpsarray = new int[Screen.width];
}
function Start() {
Time.timeScale = 1.0;
if (startPaused) {
PauseGame();
}
}
function OnPostRender() {
if (showfpsgraph && mat != null) {
GL.PushMatrix ();
GL.LoadPixelMatrix();
for (var i = 0; i < mat.passCount; ++i)
{
mat.SetPass(i);
GL.Begin( GL.LINES );
for (var x=0; x<fpsarray.length; ++x) {
GL.Vertex3(x,fpsarray[x],gldepth);
}
GL.End();
}
GL.PopMatrix();
ScrollFPS();
}
}
function ScrollFPS() {
for (var x=1; x<fpsarray.length; ++x) {
fpsarray[x-1]=fpsarray[x];
}
if (fps < 1000) {
fpsarray[fpsarray.length-1]=fps;
}
}
static function IsDashboard() {
return Application.platform == RuntimePlatform.OSXDashboardPlayer;
}
static function IsBrowser() {
return (Application.platform == RuntimePlatform.WindowsWebPlayer ||
Application.platform == RuntimePlatform.OSXWebPlayer);
}
static function IsDesktop() {
return (Application.platform == RuntimePlatform.WindowsPlayer ||
Application.platform == RuntimePlatform.OSXPlayer);
}
function LateUpdate () {
if (showfps || showfpsgraph) {
FPSUpdate();
}
if (allowPause && Input.GetKeyDown("escape"))
{
switch (currentPage) {
case Page.None: PauseGame(); break;
case Page.Main: if (!IsBeginning()) UnPauseGame(); break;
default: currentPage = Page.Main;
}
}
if (allowPause && Input.GetKeyDown("p"))
{
switch (currentPage) {
case Page.None: PauseGame(); break;
case Page.Main: if (!IsBeginning()) UnPauseGame(); break;
default: currentPage = Page.Main;
}
}
}
function OnGUI () {
if (skin != null) {
GUI.skin = skin;
}
ShowStatNums();
if (IsGamePaused()) {
GUI.color = hudColor;
switch (currentPage) {
case Page.Main: PauseMenu(); break;
case Page.Options: ShowOptions(); break;
case Page.Credits: ShowCredits(); break;
case Page.Help: ShowHelp(); break;
}
}
}
function IsLegalSrc() {
return (Application.srcValue == null) || (Application.srcValue == srcValue);
}
private var toolbarInt:int=0;
private var toolbarStrings: String[]= ["Audio","Graphics","Controls","Stats","System"];
function ShowOptions() {
BeginPage(318,300);
toolbarInt = GUILayout.Toolbar (toolbarInt, toolbarStrings);
switch (toolbarInt) {
case 0: VolumeControl(); break;
case 4: ShowDevice(); break;
case 1: QualityControl(); Qualities(); break;
case 3: StatControl(); break;
case 2: ShowHelp();
}
EndPage();
}
function ShowCredits() {
BeginPage(300,300);
for (var credit in credits) {
GUILayout.Label(credit);
}
for (var credit in crediticons) {
GUILayout.Label(credit);
}
EndPage();
}
function ShowHelp() {
for (var tip in help) {
GUILayout.Label(tip);
}
// lockCursor = GUILayout.Toggle(lockCursor,"Hide Cursor");
}
function ShowBackButton() {
GUI.contentColor = Color.white;
if (GUI.Button(Rect(332,Screen.height-126,50,20),"Back")) {
currentPage = Page.Main;
}
}
function Available(isAvailable) {
return isAvailable ? "Available" : "Not Available";
}
function ShowDevice() {
GUILayout.Label ("Unity player version "+Application.unityVersion);
GUILayout.Label("Graphics: "+SystemInfo.graphicsDeviceName+" "+
SystemInfo.graphicsMemorySize+"MB
“+
SystemInfo.graphicsDeviceVersion+”
"+
SystemInfo.graphicsDeviceVendor);
GUILayout.Label("Shadows: "+ Available(SystemInfo.supportsShadows));
GUILayout.Label("Image Effects: "+Available(SystemInfo.supportsImageEffects));
GUILayout.Label("Render Textures: "+Available(SystemInfo.supportsRenderTextures));
}
function Qualities() {
GUILayout.Label(QualitySettings.names[QualitySettings.GetQualityLevel()]);
GUILayout.Label("Pixel Light Count: "+QualitySettings.pixelLightCount);
GUILayout.Label("Shadow Cascades: "+QualitySettings.shadowCascades);
GUILayout.Label("Shadow Distance: "+QualitySettings.shadowDistance);
GUILayout.Label("Soft Vegetation: "+QualitySettings.softVegetation);
// GUILayout.Label("Sync to VBL: "+QualitySettings.syncToVBL);
// Screen.fullScreen = GUILayout.Toggle(Screen.fullScreen,"Full Screen");
}
function QualityControl() {
GUILayout.BeginHorizontal();
if (GUILayout.Button("Decrease")) {
QualitySettings.DecreaseLevel();
}
if (GUILayout.Button("Increase")) {
QualitySettings.IncreaseLevel();
}
GUILayout.EndHorizontal();
}
function VolumeControl() {
GUILayout.Label("Volume");
AudioListener.volume = GUILayout.HorizontalSlider(AudioListener.volume,0.0,1.0);
//if (!IsBrowser())
//speech = GUILayout.Toggle(speech,"Text to Speech");
}
function StatControl() {
GUILayout.BeginHorizontal();
// showtris = GUILayout.Toggle(showtris,"Triangles");
// showvtx = GUILayout.Toggle(showvtx,"Vertices");
showfps = GUILayout.Toggle(showfps,"FPS");
showfpsgraph = GUILayout.Toggle(showfpsgraph,"FPS Graph");
GUILayout.EndHorizontal();
}
function FPSUpdate() {
var delta = Time.smoothDeltaTime;
if (!IsGamePaused() && delta !=0.0) {
fps = 1 / delta;
}
}
function ShowStatNums() {
GUILayout.BeginArea(Rect(Screen.width-100,10,100,200));
if (showfps) {
var fpsString= fps.ToString ("#,##0 fps");
GUI.color = Color.Lerp(lowFPSColor, highFPSColor,(fps-lowFPS)/(highFPS-lowFPS));
GUILayout.Label (fpsString);
}
if (showtris || showvtx) {
GetObjectStats();
GUI.color = statColor;
}
if (showtris) {
GUILayout.Label (tris+"tri");
}
if (showvtx) {
GUILayout.Label (verts+"vtx");
}
GUILayout.EndArea();
}
function BeginPage(width:int,height:int) {
GUILayout.BeginArea(Rect((Screen.width-width)/2,menutop,width,height));
}
function EndPage() {
GUILayout.EndArea();
if (currentPage != Page.Main) {
ShowBackButton();
}
}
function IsBeginning() {
return Time.time < startTime;
}
function PauseMenu() {
BeginPage(200,200);
if (GUILayout.Button (IsBeginning() ? "Play" : "Continue")) {
UnPauseGame();
}
if (GUILayout.Button ("Options")) {
currentPage = Page.Options;
}
if (GUILayout.Button ("Credits")) {
currentPage = Page.Credits;
}
if (IsDesktop() && GUILayout.Button ("Quit")) {
Application.Quit();
}
if (IsBeginning()) {
GUILayout.Label(goal);
GUILayout.Label("Hit ESC key to pause");
}
EndPage();
}
function GetObjectStats() {
verts = 0;
tris = 0;
var ob = FindObjectsOfType(GameObject);
for (var obj in ob) {
if (obj.active) {
GetObjectStats(obj);
}
}
}
function GetObjectStats(object:GameObject) {
var filters : Component[];
filters = object.GetComponentsInChildren(MeshFilter);
for( var f:MeshFilter in filters )
{
tris += f.sharedMesh.triangles.Length/3;
verts += f.sharedMesh.vertexCount;
}
}
function PauseGame() {
savedTimeScale = Time.timeScale;
Time.timeScale = 0;
AudioListener.pause = true;
// if (pauseFilter) pauseFilter.enabled = true;
currentPage = Page.Main;
Screen.lockCursor = false;
}
function UnPauseGame() {
Time.timeScale = savedTimeScale;
AudioListener.pause = false;
// if (pauseFilter) pauseFilter.enabled = false;
currentPage = Page.None;
if (IsBeginning() && start != null) {
start.active = true;
}
}
static function IsGamePaused() {
return Time.timeScale==0;
}
// this used to be necessary? hopefully not anymore
function OnApplicationPause(pause:boolean) {
if (IsGamePaused()) {
AudioListener.pause = true;
}
}
The ‘CatchWater’ Script:
var Sound : AudioClip;
private var soundTriggered = false;
public var waterCaught:int=0;
function OnTriggerStay(other : Collider){
if(other.gameObject.tag=="Player"){
waterCaught++;
}
if(waterCaught >= 1000){
if (soundTriggered) return;
audio.PlayOneShot(Sound);
soundTriggered = true;
}
}
function OnGUI(){
GUI.Label(Rect(60,314,250,100),(waterCaught.ToString())+ " mL") ; // Bucket
}
function resetWaterCaught(){
waterCaught=0;
}
The ‘DropWater’ Script:
private var touchingTank:boolean=false;
function Update(){
if(touchingTank){
if (Input.GetKey (KeyCode.Return)){
tankLevel.updatetankLevel(GameObject.Find("Cloud/CloudHit").GetComponent(CatchWater).waterCaught);
GameObject.Find("Cloud/CloudHit").GetComponent(CatchWater).waterCaught=0;
}
}
}
function OnTriggerEnter(other : Collider){
if(other.gameObject.tag=="Player"){
touchingTank=true;
}
}
function OnTriggerExit(other : Collider){
if(other.gameObject.tag=="Player"){
touchingTank=false;
}
}
How do I pause the counter that determines how much rain Chelsea has collected while she is within the box-collider - a spot in the game where some rain falls, when the pause button(s) are pressed?