一、游戏介绍:
主人公在平台上跑酷,可以跳跃和双跳来跨越障碍。
主人公跑酷中可以获取金币。
游戏支持暂停和重玩。
二、游戏场景:
游戏分为2个场景:start和game。
Start场景:
这个场景很简单就一个背景图片上面一个button按钮点击进入Game场景
Game场景:
Object:
Main Camera
搭载脚本:
using UnityEngine;
public class CamerSet : MonoBehaviour {
public Camera camera;
public GameObject player;
public float cameraChangeSize = 5f;
public float sizeChangeSpeed = 2f;
public float maxSize = 10f;
public float minSize = -5f;
void Start () {
}
//保证摄像头跟随主人公且保持在一定范围内
void Update () {
cameraChangeSize = player.transform.position.y + 5f;
cameraChangeSize = cameraChangeSize >= maxSize ? maxSize : cameraChangeSize;
cameraChangeSize = cameraChangeSize <= minSize ? minSize : cameraChangeSize;
camera.orthographicSize = Mathf.Lerp(camera.orthographicSize, cameraChangeSize, Time.deltaTime * sizeChangeSpeed);
}
}
Shy搭载脚本
ScrollBG:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ScrollBG : MonoBehaviour {
public float scrollSpeed = 0.5f;
private float targetOffset;
void Start () {
}
// Update is called once per frame
void Update () {
targetOffset += Time.deltaTime * scrollSpeed;
//主纹理偏移 完成背景的自动滚动
GetComponent<Renderer>().material.mainTextureOffset = new Vector2(targetOffset, 0);
}
}
Cube_A、Cube_B:初始舞台
DiezoneA、DiezoeB:判断主人公死亡
Player:主人公
搭载脚本 PlayerControll :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerControll : MonoBehaviour {
//枚举主人公状态
public enum PlayerState {
Run,
Jump,
DoubleJump,
Die
}
public PlayerState playerState;
public float jumpPower = 200f;
public AudioClip[] Sound;
public Animator animator;
public GameManger gameManger;
public bool isDie = false;
void Start () {
// Debug.Log(gameManger.coins);
}
// Update is called once per frame
void Update () {
//检测主人公是否跳跃和双跳
if (Input.GetKeyDown(KeyCode.Space)) {
if (playerState == PlayerState.DoubleJump||playerState==PlayerState.Die) ;
else if (playerState == PlayerState.Jump)
DoubleJump();
else
Jump();
}
}
void SoundPlayer(int Index) {
GetComponent<AudioSource>().clip = Sound[Index];
GetComponent<AudioSource>().Play();
}
private void OnTriggerEnter(Collider other) {
switch (other.gameObject.name) {
case "Coin":
Destroy(other.gameObject);
GetCion();
break;
case "DiezoneA":
if (playerState != PlayerState.Die)
GameOver();
break;
case "DiezoneB":
if (playerState != PlayerState.Die)
GameOver();
break;
}
}
void GetCion() {
SoundPlayer(0);
gameManger.GetCoins();
}
private void OnCollisionEnter(Collision collision) {
if (playerState != PlayerState.Run&&playerState!=PlayerState.Die) Run();
}
void Run() {
playerState = PlayerState.Run;
animator.SetBool("isInGroud", true);
}
void Jump() {
playerState = PlayerState.Jump;
this.GetComponent<Rigidbody>().AddForce(new Vector3(0, jumpPower, 0));
SoundPlayer(1);
animator.SetTrigger("Jump");
animator.SetBool("isInGroud", false);
}
void DoubleJump() {
playerState = PlayerState.DoubleJump;
this.GetComponent<Rigidbody>().AddForce(new Vector3(0, jumpPower, 0));
SoundPlayer(1);
animator.SetTrigger("DoubleJump");
animator.SetBool("isInGroud", false);
}
void GameOver() {
playerState = PlayerState.Die;
isDie = true;
SoundPlayer(2);
}
public string GetPlayerState (){
return playerState.ToString();
}
}
主人公动画控制器:
GameManger 游戏管理
脚本BlockLoop:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlockLoop : MonoBehaviour {
public float speed = 3f;
public GameObject aZone;
public GameObject bZone;
public GameObject[] blockGameObjects;//制作好的各种地形带金币
void Start () {
}
// Update is called once per frame
void Update () {
MoveBlock();
}
//舞台的转动
void MoveBlock() {
aZone.transform.Translate(Vector3.left * speed * Time.deltaTime);
bZone.transform.Translate(Vector3.left * speed * Time.deltaTime);
if (bZone.transform.position.x <= 0) {
Destroy(aZone);
aZone = bZone;
MakeNewBlock();
}
}
//创建新舞台
void MakeNewBlock() {
int blockIndex = Random.Range(0, blockGameObjects.Length);
bZone = Instantiate(blockGameObjects[blockIndex], new Vector3(aZone.transform.position.x+30, -5, 0), transform.rotation) as GameObject;
}
}
脚本GameManger :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManger : MonoBehaviour {
private float runSpeed = 10f;
private float runMeter = 0f;
private float coins = 0;
private PlayerControll playerControll;
private bool isPause;
public Text coinText;
public Text runText;
public Text resultCoinText;
public Text resultRunText;
public Image resultImage;
private void Awake() {
//隐藏表示结果的ui
resultImage.gameObject.SetActive(false);
playerControll = GameObject.Find("Player").GetComponent<PlayerControll>();
isPause = false;
}
void Update() {
if (playerControll.isDie) {
resultImage.gameObject.SetActive(true);
return;
}
runMeter += Time.deltaTime * runSpeed;
runText.text = "距离 " + (int)runMeter;
resultRunText.text = "" + (int)runMeter;
}
public void GetCoins() {
resultCoinText.text=coinText.text = ""+(++coins);
}
//replay加载到Start场景
public void Replay() {
Application.LoadLevel("start");
}
//游戏暂停的实现
public void Pause() {
if (Input.GetKeyDown(KeyCode.Space)) return;
isPause = !isPause;
if (isPause)
Time.timeScale = 0;
else
Time.timeScale = 1;
}
}
UI设计: