#pragma strict
public var movementSpeed : float = 5.0f;
private var isGrounded : boolean = false;
function Start () {
}
function Update () {
rigidbody.velocity = new Vector3(0, rigidbody.velocity.y, 0); //Set X and Z velocity to 0
transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * movementSpeed, 0, 0);
/*if (Input.GetButtonDown("Jump") && isGrounded)
{
Jump(); //Manual jumping
}*/
}
function Jump()
{
if (!isGrounded) { return; }
isGrounded = false;
rigidbody.velocity = new Vector3(0, 0, 0);
rigidbody.AddForce(new Vector3(0, 700, 0), ForceMode.Force);
}
function FixedUpdate()
{
isGrounded = Physics.Raycast(transform.position, -Vector3.up, 1.0f);
if (isGrounded)
{
Jump(); //Automatic jumping
}
}
2013년 6월 17일 월요일
GameControll
#pragma strict
public enum GameState { playing, gameover };
public var platformPrefab : Transform;
public static var gameState:GameState;
private var playerTrans:Transform ;
private var platformsSpawnedUpTo :float = 0;
private var platforms: ArrayList ;
private var nextPlatformCheck:float = 0.0f;
function Awake () {
playerTrans = GameObject.FindGameObjectWithTag("Player").transform;
platforms = new ArrayList();
SpawnPlatforms(25.0);
StartGame();
}
function Start ()
{
Time.timeScale = 1.0f;
gameState = GameState.playing;
}
function StartGame()
{
Time.timeScale = 1.0f;
gameState = GameState.playing;
}
function GameOver()
{
Time.timeScale = 0.0f; //Pause the game
gameState = GameState.gameover;
GameGUI.SP.CheckHighscore();
}
function Update () {
//Do we need to spawn new platforms yet? (we do this every X meters we climb)
var playerHeight:float = playerTrans.position.y;
if (playerHeight > nextPlatformCheck)
{
PlatformMaintenaince(); //Spawn new platforms
}
//Update camera position if the player has climbed and if the player is too low: Set gameover.
var currentCameraHeight:float = transform.position.y;
var newHeight:float = Mathf.Lerp(currentCameraHeight, playerHeight, Time.deltaTime * 10);
if (playerTrans.position.y > currentCameraHeight)
{
transform.position = new Vector3(transform.position.x, newHeight, transform.position.z);
}else{
//Player is lower..maybe below the cameras view?
if (playerHeight < (currentCameraHeight - 10))
{
GameOver();
}
}
//Have we reached a new score yet?
if (playerHeight > GameGUI.score)
{
GameGUI.score = playerHeight;
}
}
function PlatformMaintenaince()
{
nextPlatformCheck = playerTrans.position.y + 10;
//Delete all platforms below us (save performance)
for(var i:int = platforms.Count-1;i>=0;i--)
{
var plat:Transform = platforms[i];
if (plat.position.y < (transform.position.y - 10))
{
Destroy(plat.gameObject);
platforms.RemoveAt(i);
}
}
//Spawn new platforms, 25 units in advance
SpawnPlatforms(nextPlatformCheck + 25);
}
function SpawnPlatforms(upTo:float )
{
var spawnHeight:float = platformsSpawnedUpTo;
while (spawnHeight <= upTo)
{
var x:float = Random.Range(-10.0f, 10.0f);
var pos :Vector3 = new Vector3(x, spawnHeight, 12.0f);
var plat:Transform = Instantiate(platformPrefab, pos, Quaternion.identity);
platforms.Add(plat);
spawnHeight += Random.Range(1.6f, 3.5f);
}
platformsSpawnedUpTo = upTo;
}
public enum GameState { playing, gameover };
public var platformPrefab : Transform;
public static var gameState:GameState;
private var playerTrans:Transform ;
private var platformsSpawnedUpTo :float = 0;
private var platforms: ArrayList ;
private var nextPlatformCheck:float = 0.0f;
function Awake () {
playerTrans = GameObject.FindGameObjectWithTag("Player").transform;
platforms = new ArrayList();
SpawnPlatforms(25.0);
StartGame();
}
function Start ()
{
Time.timeScale = 1.0f;
gameState = GameState.playing;
}
function StartGame()
{
Time.timeScale = 1.0f;
gameState = GameState.playing;
}
function GameOver()
{
Time.timeScale = 0.0f; //Pause the game
gameState = GameState.gameover;
GameGUI.SP.CheckHighscore();
}
function Update () {
//Do we need to spawn new platforms yet? (we do this every X meters we climb)
var playerHeight:float = playerTrans.position.y;
if (playerHeight > nextPlatformCheck)
{
PlatformMaintenaince(); //Spawn new platforms
}
//Update camera position if the player has climbed and if the player is too low: Set gameover.
var currentCameraHeight:float = transform.position.y;
var newHeight:float = Mathf.Lerp(currentCameraHeight, playerHeight, Time.deltaTime * 10);
if (playerTrans.position.y > currentCameraHeight)
{
transform.position = new Vector3(transform.position.x, newHeight, transform.position.z);
}else{
//Player is lower..maybe below the cameras view?
if (playerHeight < (currentCameraHeight - 10))
{
GameOver();
}
}
//Have we reached a new score yet?
if (playerHeight > GameGUI.score)
{
GameGUI.score = playerHeight;
}
}
function PlatformMaintenaince()
{
nextPlatformCheck = playerTrans.position.y + 10;
//Delete all platforms below us (save performance)
for(var i:int = platforms.Count-1;i>=0;i--)
{
var plat:Transform = platforms[i];
if (plat.position.y < (transform.position.y - 10))
{
Destroy(plat.gameObject);
platforms.RemoveAt(i);
}
}
//Spawn new platforms, 25 units in advance
SpawnPlatforms(nextPlatformCheck + 25);
}
function SpawnPlatforms(upTo:float )
{
var spawnHeight:float = platformsSpawnedUpTo;
while (spawnHeight <= upTo)
{
var x:float = Random.Range(-10.0f, 10.0f);
var pos :Vector3 = new Vector3(x, spawnHeight, 12.0f);
var plat:Transform = Instantiate(platformPrefab, pos, Quaternion.identity);
platforms.Add(plat);
spawnHeight += Random.Range(1.6f, 3.5f);
}
platformsSpawnedUpTo = upTo;
}
GameGUI
#pragma strict
public static var SP : GameGUI ;
public static var score: int ;
private var bestScore:int = 0;
function Awake()
{
SP = this;
score = 0;
bestScore = PlayerPrefs.GetInt("BestScorePlatforms", 0);
}
function OnGUI()
{
GUILayout.Space(3);
GUILayout.Label(" Score: " + score);
GUILayout.Label(" Highscore: " + bestScore);
if (GameState.gameState == GameState.gameover)
{
GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.BeginVertical();
GUILayout.FlexibleSpace();
GUILayout.Label("Game over!");
if (score > bestScore)
{
GUI.color = Color.red;
GUILayout.Label("New highscore!");
GUI.color = Color.white;
}
if (GUILayout.Button("Try again"))
{
Application.LoadLevel(Application.loadedLevel);
}
GUILayout.FlexibleSpace();
GUILayout.EndVertical();
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
}
public function CheckHighscore()
{
if (score > bestScore)
{
PlayerPrefs.SetInt("BestScorePlatforms", score);
}
}
public static var SP : GameGUI ;
public static var score: int ;
private var bestScore:int = 0;
function Awake()
{
SP = this;
score = 0;
bestScore = PlayerPrefs.GetInt("BestScorePlatforms", 0);
}
function OnGUI()
{
GUILayout.Space(3);
GUILayout.Label(" Score: " + score);
GUILayout.Label(" Highscore: " + bestScore);
if (GameState.gameState == GameState.gameover)
{
GUILayout.BeginArea(new Rect(0, 0, Screen.width, Screen.height));
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
GUILayout.BeginVertical();
GUILayout.FlexibleSpace();
GUILayout.Label("Game over!");
if (score > bestScore)
{
GUI.color = Color.red;
GUILayout.Label("New highscore!");
GUI.color = Color.white;
}
if (GUILayout.Button("Try again"))
{
Application.LoadLevel(Application.loadedLevel);
}
GUILayout.FlexibleSpace();
GUILayout.EndVertical();
GUILayout.FlexibleSpace();
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
}
public function CheckHighscore()
{
if (score > bestScore)
{
PlayerPrefs.SetInt("BestScorePlatforms", score);
}
}
피드 구독하기:
글 (Atom)