这款游戏是果App Store、安卓系统的游戏,喜欢怀旧复古的朋友有兴趣可以试着做一下,游戏虽然简单,但重要的是学习制作游戏的过程。

一.将图片导入到UI中 更改Type为2DandUI,保存

二.将摄像机该为2D, 改为正交模式
![]_Z}7PBFG3S.png](https://img.haomeiwen.com/i3912807/0354b5dd41d83c36.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
三.添加一个Quad改名为BG,再添加一个材质导入图片做出背景(注意为了配合各个设备的使用,所以我们通常将分辨率改为1200*800)

四.创建两根物体为障碍物这里创建一个空物体,将空物体的名称改为Pipe,然后将障碍物放入Pipe中。因为是上下两根障碍物所以我们将上下两根障碍物分别取名为Pipe_Up和Pipe_Down表示,材质的Shader改为 Unlit的Transparent,障碍物的Z轴为-1



五.创建一个Back为底部,为了在游戏中player接触到底部(代表死亡)可以有触发结束游戏

六.将BG放入Prefabs中成为预制物,并复制作为镜头可以滑动的对象


七.在BG中添加碰撞器,当Player接触到碰撞器后,使物体在游戏运行后可以连续替换面板做到一直在场景中进行游戏

添加脚本
public Transform currentBg;
public Pipe_Test pipe1;
public Pipe_Test pipe2;
void OnTriggerEnter(Collider collider)
{
if (collider.tag == "Player")
{
Debug.Log("13");
Transform firstBg = Gamemanager.Instances.firstBg;//第一个游戏画面
//当前的面板位置=第一个面板位置+自身的大小
currentBg.position = new Vector3(firstBg.position.x + 15, currentBg.position.y, currentBg.position.z);
Gamemanager.Instances.firstBg = currentBg;
pipe1.RangerNum();
pipe2.RangerNum();
}
}

八.创建一个Player作为主人物除了根据自己的场景修改尺寸外还要在预制中修改模式(注意在预制中显示的动画 当显示第一帧就是0,第二帧为0.33,第三帧为0.66),并把Colider干掉



九.并给Player添加脚本 实现player的动画显示
public float timer = 0;
public int frameNumber = 15;//一秒10帧
public int frameCount = 0;//当前的帧数
public bool animation = false;//是否播放播放动画
public bool canjump = false;//是否跳跃
void Start()
{
}
void Update()
{
if (Gamemanager.Instances.GameState == Gamemanager.GameState_PLAYING)
{
timer += Time.deltaTime;//计算器整长
if (timer >= 1.0f / frameNumber)//当这个时间大于一秒时间
{
frameCount++;//当前的帧增加
timer -= 1.0f / frameNumber;
int frameIndex = frameCount % 3;//当前是第几帧
GetComponent<Renderer>().material.SetTextureOffset("_MainTex", new Vector2(0.333f * frameIndex, 0));
}
}
if (Gamemanager.Instances.GameState == Gamemanager.GameState_PLAYING)
{
if (Input.GetMouseButton(0))
{
GetComponent<AudioSource>().Play();
Vector3 v2 = GetComponent<Rigidbody>().velocity;
GetComponent<Rigidbody>().velocity = new Vector3(v2.x,5f,0);
}
}
}
public void getLife()
{
//给小鸟添加重力
GetComponent<Rigidbody>().useGravity = true;
GetComponent<Rigidbody>().velocity = new Vector3(5, 0, 0);
}
给Player添加刚体,并在刚体中控制物体的坐标轴,这样可以使物体在死亡后不会使摄像机产生旋转不会使人有头晕的不适

十.设置得分系统
在摄像机中的脚本中添加设置得分

给Player创建脚本
void Start()
{
RangerNum();
}
void Update()
{
}
//柱子变化
public void RangerNum()
{
float y = Random.Range(-0.74f, -0.494f);
this.transform.localPosition = new Vector3(this.transform.localPosition.x, y, this.transform.localPosition.z);
}
//添加分数
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
Debug.Log("xxxxxxx");
Gamemanager.Instances.score++;
GetComponent<AudioSource>().Play();
}
}
void OnGUI()
{
GUILayout.Label("当前得分:" + Gamemanager.Instances.score.ToString());
}
十一.设置游戏状态 当Player进入游戏、进行游戏、Player死亡状态使使用的脚本
//拿到最后一张图
public Transform firstBg;
//单例模式
public static Gamemanager Instances;
private GameObject bird;
public int GameState = GameState_MENU;
////游戏菜单状态,游戏中,游戏结束
public static int GameState_MENU = 0; //游戏菜单状态
public static int GameState_PLAYING = 1; //游戏中状态
public static int GameState_Paser = 2;
public static int GameState_END = 3;
public GameObject a;
//积分
public int score = 0;
void Awake()
{
Instances = this;
bird = GameObject.FindGameObjectWithTag("Player");
}
void Update()
{
if (GameState == GameState_MENU)
{
if (Input.GetMouseButtonDown(0))
{
GameState = GameState_PLAYING;
//给小鸟添加重力
bird.GetComponent<Rigidbody>().useGravity = true;
//调用getlife方法
bird.SendMessage("getLife");
}
//}
print(GameState);
//假如游戏状态==结束
}
if (GameState == Gamemanager.GameState_END)
{
//结束面板显示
GameMenu_Test.instance.gameObject.SetActive(true);
GameMenu_Test.instance.UpdateScore(score);
a.SetActive(true);
}
}
在障碍物中添加
public class PipeUpOrDown_Test : MonoBehaviour {
void OnCollisionEnter(Collision other)
{
print("OnCollisionEnter");
if (other.gameObject.tag == "Player")
{
Gamemanager.Instances.GameState = Gamemanager.GameState_END;
//检测
Debug.Log("xxxx");
}
}
}
十二.添加音量
在需要使用音量的地方添加Audio Sourse(简单的部分就不细说了一张图表示)

十三.设置菜单栏(死亡后的得分统计和重新开始游戏的面板)
都是创建的空物体 Score添加GUI Texture 、HowScore和HighScore添加GUI Text、Start是一个Button按钮


添加脚本
public class GameMenu_Test : MonoBehaviour {
public static GameMenu_Test instance;
public GUIText nowScore;
public GUIText highScore;
public GUITexture startTexture;
//隐藏菜单栏
void Awake()
{
}
void Start()
{
instance = this;
this.gameObject.SetActive(false);
}
public void UpdateScore(float nowScore)
{
float highScore = PlayerPrefs.GetFloat("score", 0);
if (nowScore > highScore)
{
highScore = nowScore;
}
PlayerPrefs.SetFloat("score", highScore);
this.nowScore.text = nowScore + "";
this.highScore.text = highScore + "";
if (Input.GetMouseButtonDown(0) && Gamemanager.Instances.GameState == Gamemanager.GameState_END)
{
Rect rect = startTexture.GetScreenRect();
Vector3 mousePos = Input.mousePosition;
if (mousePos.x > rect.x && mousePos.x < (rect.x + rect.width) && mousePos.y > rect.y && mousePos.y < (rect.y + rect.height))
{
Application.LoadLevel(0);
}
}
}
}
十四.控制摄像机上下移动范围(让摄像机跟随物体上下移动不会感觉死板的只是在跟随Player进行游戏)
public class FollowPlayer_Test : MonoBehaviour {
public GameObject Bird;
private Transform BirdTransform;
void Start ()
{
Bird = GameObject.FindGameObjectWithTag("Player");
BirdTransform = Bird.transform;
}
void Update ()
{
Vector3 BirdPos = BirdTransform.position;
float y = BirdPos.y - 3.5088f;
if (y > 3.38f)
{
y = 3.38f;
}
if (y < 1.87f)
{
y = 1.87f;
}
this.transform.position = new Vector3(BirdPos.x + 0.29f,y, -10);
}
}

网友评论