Skip to content

Instantly share code, notes, and snippets.

@HansNewbie

HansNewbie/GUIManager.cs

Last active Aug 5, 2016
Embed
What would you like to do?
Improvements to Unity Endless Runner tutorial from http://catlikecoding.com/unity/tutorials/runner/; Find more information about my version at http://ideascomecheap.blogspot.com/search/label/Unity%20Endless%20Runner%20Tutorial
// This is a new file that is not mentioned in the tutorial
//
// To use this, create a new empty under manager with this attached
//
// This is created so that Android application can be exited by pressing the back button
using UnityEngine;
public class GameExitManager : MonoBehaviour {
void Update () {
if (Input.GetKeyDown("escape")) {
Application.Quit();
}
}
}
using UnityEngine;
public class GUIManager : MonoBehaviour {
public GUIText boostsText, distanceText, gameOverText, instructionsText, runnerText;
private static GUIManager instance;
void Start() {
instance = this;
GameEventManager.GameStart += GameStart;
GameEventManager.GameOver += GameOver;
gameOverText.enabled = false;
// Hans
boostsText.enabled = false;
distanceText.enabled = false;
if (Application.platform == RuntimePlatform.Android) {
instructionsText.text = "Tap to play";
}
}
void Update() {
if ((Input.GetButtonDown("Jump")) ||
((Application.platform == RuntimePlatform.Android) && (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))) {
GameEventManager.TriggerGameStart();
}
}
private void GameStart() {
gameOverText.enabled = false;
instructionsText.enabled = false;
runnerText.enabled = false;
// Hans
boostsText.enabled = true;
distanceText.enabled = true;
enabled = false;
}
private void GameOver() {
gameOverText.enabled = true;
instructionsText.enabled = true;
enabled = true;
}
public static void SetBoosts(int boosts) {
instance.boostsText.text = boosts.ToString();
}
public static void SetDistance(float distance) {
instance.distanceText.text = distance.ToString("f0");
}
}
using UnityEngine;
public class Runner : MonoBehaviour {
public static float distanceTraveled;
public float acceleration;
public Vector3 boostVelocity, jumpVelocity;
public float gameOverY;
private bool touchingPlatform;
private Vector3 startPosition;
private static int boosts;
void Start() {
GameEventManager.GameStart += GameStart;
GameEventManager.GameOver += GameOver;
startPosition = transform.localPosition;
renderer.enabled = false;
rigidbody.isKinematic = true;
enabled = false;
}
private void GameStart () {
boosts = 0;
GUIManager.SetBoosts(boosts);
distanceTraveled = 0f;
GUIManager.SetDistance(distanceTraveled);
transform.localPosition = startPosition;
renderer.enabled = true;
rigidbody.isKinematic = false;
enabled = true;
}
private void GameOver() {
renderer.enabled = false;
rigidbody.isKinematic = true;
enabled = false;
}
void Update () {
if ((Input.GetButtonDown("Jump")) ||
((Application.platform == RuntimePlatform.Android) && (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began))) {
Jump ();
}
distanceTraveled = transform.localPosition.x;
GUIManager.SetDistance (distanceTraveled);
if (transform.localPosition.y < gameOverY) {
GameEventManager.TriggerGameOver();
}
}
void FixedUpdate() {
if (touchingPlatform) {
rigidbody.AddForce(acceleration, 0f, 0f, ForceMode.Acceleration);
}
}
void OnCollisionEnter() {
touchingPlatform = true;
}
void OnCollisionExit() {
touchingPlatform = false;
}
public static void AddBoost() {
boosts += 1;
GUIManager.SetBoosts(boosts);
}
private void Jump() {
if (touchingPlatform) {
rigidbody.AddForce(jumpVelocity, ForceMode.VelocityChange);
touchingPlatform = false; // This allow one jump after colliding with side of platform, but not more
}
else if (boosts > 0) {
rigidbody.AddForce(boostVelocity, ForceMode.VelocityChange);
boosts -= 1;
GUIManager.SetBoosts (boosts);
}
}
}
@becker88

This comment has been minimized.

Copy link

@becker88 becker88 commented Aug 5, 2016

Amazing project....!! Good

@becker88

This comment has been minimized.

Copy link

@becker88 becker88 commented Aug 5, 2016

I need this idea...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.