一、太空大战需求
1、背景
2、角色
我方飞机、敌方飞机、敌方陨石、子弹
3、功能:
(1).背景
a.循环滚动
(2).我方飞机
a.键盘控制移动
b.鼠标左键发射子弹
c.生命功能,碰到敌机及子弹,陨石扣生命值
(3).敌方陨石
a.程序启动3秒后随机克隆3个,然后每个6秒克隆3个
b.陨石自身旋转并向下移动
d.碰到我方子弹,飞机或边缘碰撞器自动销毁
(4).敌方飞机
a.程序开启,每隔8秒随机克隆3个
b.每隔1秒自动发射子弹
c.被我方子弹击中或我方飞机触碰,摧毁
d.碰到边缘碰撞器摧毁
e.自动向前方向移动
(5)、子弹
a.子弹移动
b.子弹碰到敌机,陨石,我方飞机,边缘触发器就摧毁
3.特效
(1).我方飞机尾部喷射特效
(2).发射子弹有声音播放
(3).摧毁伴随爆炸声音以及爆炸特效
4、技术(基于unity开发)
(1)键盘事件
(2)鼠标事件
(3)克隆、销毁
(4)协程
(5)碰撞检测
(6)音频
(7)Mathf类、Transform类、Time类、GameObject类、Vector3类、Random类
二、场景
1、背景
通过两个Quad拼接而成
Quad由两个三角形拼成的四边形,当场景对象被作为简单显示图片或者影片的屏幕时,使用Quad时很合适的。然后添加太空图片。
2、边缘碰撞器
新建一个Cube,将除Transform以外的组件全部移除,然后添加Box Collider碰撞组件,点击Edit Collider修改绿色框框的大小,形成边缘碰撞器(别忘记勾选Is Trigger(触发器))
3、角色
在网上找素材。
三、代码部分
(由于初学,没有私有,所以很多东西都是拖拽的)
1、背景循环滚动
public float speed = 5;//速度
private Vector3 startPos;//初始位置
void Start ()
{startPos = transform.position;
}
void Update ()
{
//背景滚屏效果
float pos = Mathf.Repeat(speed*Time.time,30);
transform.position =startPos+ Vector3.back*pos;
}
(a)、Mathf.Repeat:重复
循环数值t,0到length长度之间,t的值永远不会大于length的值,也永远不会小于0。
(b)、Time.time:时间
本帧开始的时间(只读)。这是以秒为单位的时间,从游戏开始。
2、玩家
(1)、玩家移动,射击
public float speed = 10;//移动速度
public GameObject playerBullet;//玩家子弹预设体
public Transform point;//子弹发射位置
private float shotSpace = 0.25f;//发射子弹间隔
private float nextShot;//下次射击时间
void Update ()
{
Move();
Fire();
}
/// <summary>
/// 玩家移动
/// </summary>
void Move()
{
float h = Input.GetAxis("Horizontal");//水平
float v = Input.GetAxis("Vertical");//垂直
Vector3 pos = new Vector3(h,0,v);
transform.Translate(pos*Time.deltaTime*speed);
//x:-6.7 6.7 z:-13 10(该项目的范围)
//限制飞机飞行范围
float x = Mathf.Clamp(transform.position.x,-6.7f,6.7f);
float z = Mathf.Clamp(transform.position.z,-11.5f,12f);
transform.position = new Vector3(x,transform.position.y,z);}
/// <summary>
/// 玩家射击
/// </summary>
void Fire()
{
//每个0.25秒发射一颗子弹
if (Input.GetMouseButtonDown(0) && Time.time>nextShot){nextShot = Time.time + shotSpace;//克隆子弹Instantiate(playerBullet,point.position,Quaternion.identity); }
}
(a)、Mathf.Clamp:限制范围
限制value的值在min,max之间。
(2)、玩家子弹移动,触碰边缘碰撞器摧毁
public float speed = 5f;
void Update () {transform.Translate(Vector3.forward*Time.deltaTime*speed);
}/// <summary>/// 子弹碰到边框摧毁(采用触发器)/// </summary>/// <param name="c"></param>
void OnTriggerExit(Collider c){//注意要手动设置标签(边缘碰撞器)if (c.gameObject.CompareTag("Border")){Destroy(gameObject); }}//用OnTriggerExit的原因子弹一直碰撞盒子内,只有当离开盒子碰撞器的时候消失
3、游戏管理部分
public GameObject[] asteroids;//陨石(3种)public GameObject gameOverPanel;//游戏结束界面public Text hpTxt, scoreTxt;//记录玩家生命,分数文本public int hp = 3, score = 0;public GameObject enemyShip;//敌机//Unity单例模式public static GameManage instance;void Start(){instance = this;//协程 克隆陨石和敌机StartCoroutine(Clone());StartCoroutine(CloneEnemyShip());gameOverPanel.SetActive(false);}/// <summary>/// 克隆陨石/// </summary>/// <returns></returns>IEnumerator Clone(){yield return new WaitForSeconds(3f);for (int i = 0; i < 3; i++){int index = Random.Range(0, asteroids.Length);int x = Random.Range(-6, 6);int z = Random.Range(10, 12);Instantiate(asteroids[index], new Vector3(x, 0.168f, z), Quaternion.identity);}while (true){yield return new WaitForSeconds(6);for (int i = 0; i < 3; i++){int index = Random.Range(0, asteroids.Length);float x = Random.Range(-6, 6);float z = Random.Range(10, 14);Instantiate(asteroids[index], new Vector3(x, 0.168f, z), Quaternion.identity);}}}/// <summary>/// 克隆敌机/// </summary>/// <returns></returns>IEnumerator CloneEnemyShip(){while (true){yield return new WaitForSeconds(8f);for (int i = 0; i < 3; i++){int x = Random.Range(-6, 6);int z = Random.Range(10, 12);Instantiate(enemyShip, new Vector3(x, 0.168f, z), Quaternion.Euler(0,180,0));}}}void Update(){hpTxt.text = hp.ToString();scoreTxt.text = score.ToString();if (hp <= 0){Time.timeScale = 0;//游戏暂停gameOverPanel.SetActive(true);}}public void Restart(){Time.timeScale = 1;//游戏重新开始gameOverPanel.SetActive(false);hp = 3;score = 0;SceneManager.LoadScene("Game");}public void Quit(){//跳转场景(要把场景发布一下)//using UnityEngine.SceneManagement;SceneManager.LoadScene("Start");}
4、陨石部分
public GameObject explosion_asteroid;//陨石摧毁特效
public float speed = 5f;
void Update () {transform.Translate(Vector3.back*Time.deltaTime*speed);transform.Rotate(Vector3.forward,1f);
}
void OnTriggerExit(Collider c){if (c.gameObject.CompareTag("Border")){Destroy(gameObject);}}/// <summary>/// 子弹碰到陨石,玩家碰到陨石/// </summary>/// <param name="c"></param>
void OnTriggerEnter(Collider c){if (c.gameObject.CompareTag("PlayerBullet")){Destroy(c.gameObject);Destroy(gameObject);GameObject explosion = Instantiate(explosion_asteroid, c.transform.position, Quaternion.identity);Destroy(explosion, 2);//玩家加分GameManage.instance.score++;}else if (c.gameObject.CompareTag("Player")){Destroy(gameObject);GameObject explosion = Instantiate(explosion_asteroid, c.transform.position, Quaternion.identity);Destroy(explosion, 2);//玩家减血GameManage.instance.hp--;}
}
5、敌机部分
public float speed = 5f;public GameObject EnemyBullet;//敌机子弹public GameObject explosion_enemy;//敌机爆炸特效void Start () {StartCoroutine(Fire());} void Update () {Move();}void Move(){transform.Translate(Vector3.forward * Time.deltaTime * speed);}IEnumerator Fire(){while (true){yield return new WaitForSeconds(1f);Instantiate(EnemyBullet,transform.GetChild(0).position,Quaternion.identity);} }/// <summary>/// 敌机碰到边界摧毁/// </summary>/// <param name="c"></param>void OnTriggerExit(Collider c){if (c.gameObject.CompareTag("Border")){Destroy(gameObject);}}/// <summary>/// 敌机碰到玩家及其子弹/// </summary>/// <param name="c"></param>void OnTriggerEnter(Collider c){if (c.gameObject.CompareTag("PlayerBullet")){Destroy(c.gameObject);Destroy(gameObject);GameObject explosion = Instantiate(explosion_enemy, transform.position, Quaternion.identity);Destroy(explosion, 1f);GameManage.instance.score += 10;}else if (c.gameObject.CompareTag("Player")){Destroy(gameObject);GameObject explosion = Instantiate(explosion_enemy, transform.position, Quaternion.identity);Destroy(explosion, 1f);GameManage.instance.hp--;}}
四、演示图
该项目于Unity 5.6.7版本写的。