Last active
August 5, 2016 23:29
-
-
Save HansNewbie/10005219 to your computer and use it in GitHub Desktop.
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Amazing project....!! Good