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; | |
using System; | |
using System.Collections; | |
public class GUIManager : MonoBehaviour { | |
public GUIText boostsText, distanceText, gameOverText, instructionsText, runnerText, highScoresText; | |
public GUITexture boostsSign; | |
private static GUIManager instance; | |
void Start() { | |
instance = this; | |
GameEventManager.GameStart += GameStart; | |
GameEventManager.GameOver += GameOver; | |
gameOverText.enabled = false; | |
// Hans | |
boostsText.enabled = false; | |
boostsSign.enabled = false; | |
distanceText.enabled = false; | |
highScoresText.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 | |
highScoresText.enabled = false; | |
boostsText.enabled = true; | |
boostsSign.enabled = true; | |
distanceText.enabled = true; | |
enabled = false; | |
} | |
private void GameOver() { | |
boostsText.enabled = false; | |
boostsSign.enabled = false; | |
distanceText.enabled = false; | |
gameOverText.enabled = true; | |
instructionsText.enabled = true; | |
highScoresText.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"); | |
} | |
public static void SetHighScoreBoard(ArrayList highScores) { | |
string highScoreBoard = "High Scores\n"; | |
for (int i = 0; i < highScores.Count; i++) { | |
highScoreBoard += (i+1).ToString() + ". "; | |
highScoreBoard += highScores[i].ToString(); | |
highScoreBoard += "\n"; | |
} | |
instance.highScoresText.text = highScoreBoard; | |
} | |
} |
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; | |
using System; | |
using System.Collections; | |
public class HighScoreSaveLoadManager : MonoBehaviour { | |
public int noOfHighScores; | |
private ArrayList highScores = new ArrayList(); | |
private static HighScoreSaveLoadManager instance; | |
void Start () { | |
instance = this; | |
LoadHighScore(); | |
} | |
public static int GetHighScoresQuota() { | |
return instance.noOfHighScores; | |
} | |
public static ArrayList GetHighScores() { | |
return instance.highScores; | |
} | |
public static void SetHighScores(ArrayList updatedHighScore) { | |
instance.highScores = updatedHighScore; | |
SaveHighScore(); | |
} | |
private static void SaveHighScore() { | |
for (int i = 0; i < instance.highScores.Count; i++) { | |
PlayerPrefs.SetInt("HighScore"+i.ToString(), (int) instance.highScores[i]); | |
} | |
} | |
private static void LoadHighScore() { | |
ArrayList loadHighScores = new ArrayList(); | |
int counter = 0; | |
while (true) { | |
if (PlayerPrefs.HasKey("HighScore"+counter.ToString())) { | |
loadHighScores.Add(PlayerPrefs.GetInt("HighScore"+counter.ToString())); | |
counter++; | |
} | |
else { | |
break; | |
} | |
} | |
instance.highScores = loadHighScores; | |
} | |
} |
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; | |
using System; | |
using System.Collections; | |
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() { | |
RecordScoreIfNecessary(); | |
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); | |
} | |
} | |
public void RecordScoreIfNecessary() { | |
int roundedScore = (int) Math.Round(distanceTraveled); | |
ArrayList highScores = HighScoreSaveLoadManager.GetHighScores(); | |
int highScoresQuota = HighScoreSaveLoadManager.GetHighScoresQuota(); | |
highScores.Add(roundedScore); | |
highScores.Sort(); | |
highScores.Reverse(); | |
if (highScores.Count > highScoresQuota) { | |
highScores.RemoveRange(highScoresQuota, highScores.Count - highScoresQuota); | |
} | |
// for (int i = 0; i < highScores.Count; i++) { | |
// Debug.Log(highScores[i]); | |
// } | |
GUIManager.SetHighScoreBoard(highScores); | |
HighScoreSaveLoadManager.SetHighScores(highScores); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Amazing project....!! Good