导入素材

添加拖尾特效


添加碰撞盒组件


给玩家一个player标签:
用于后续判断碰撞。

- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- ///
- /// 控制角色移动、生命、动画等
- ///
- public class playerController : MonoBehaviour
- {
- public float speed = 5f;//移动速度
-
- // Start is called before the first frame update
- void Start()
- {
-
- }
-
- // Update is called once per frame
- void Update()
- {
- float moveX = Input.GetAxisRaw("Horizontal");//控制水平移动方向 A:-1 D:1 不按: 0
- float moveY = Input.GetAxisRaw("Vertical");//控制垂直移动方向 W:-1 S:1 不按: 0
-
- Vector2 position = transform.position;//定义角色位置向量
- position.x += moveX * speed * Time.deltaTime;
- position.y += moveY * speed * Time.deltaTime;
- transform.position = position;//将位置信息传输给角色
- }
- }
导入素材

添加拖尾

添加移动脚本

- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class bulletMove : MonoBehaviour
- {
- public float speed = 5f;//移动速度
- // Start is called before the first frame update
- void Start()
- {
-
- }
-
- // Update is called once per frame
- void Update()
- {
- transform.Translate(transform.up * speed * Time.deltaTime);
- }
- }
为子弹添加碰撞盒


给玩家导弹一个bullet标签:
用于判断碰撞

按下空格键生成子弹预制体
飞机脚本:
- using System.Collections.Generic;
- using UnityEngine;
-
- public class playerController : MonoBehaviour
- {
- public float speed = 5f;//移动速度
- public GameObject bullet;//子弹
- // Start is called before the first frame update
- void Start()
- {
-
- }
-
- // Update is called once per frame
- void Update()
- {
- float moveX = Input.GetAxisRaw("Horizontal");//控制水平移动方向 A:-1 D:1 不按: 0
- float moveY = Input.GetAxisRaw("Vertical");//控制垂直移动方向 W:-1 S:1 不按: 0
-
- Vector2 position = transform.position;//定义角色位置向量
- position.x += moveX * speed * Time.deltaTime;
- position.y += moveY * speed * Time.deltaTime;
- transform.position = position;//将位置信息传输给角色
-
- bullet.transform.position = transform.position;//将角色信息传输给子弹
-
- if (Input.GetKeyDown(KeyCode.Space))//按下空格键
- {
- Instantiate(bullet);//生成子弹预制体
- }
- }
- }
设置子弹最久飞行时间,过了时间自动销毁预制体
子弹脚本:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class bulletMove : MonoBehaviour
- {
- public float speed = 5f;//子弹飞行速度
- public float DestroyTime = 5f;//子弹最大飞行时间
- // Start is called before the first frame update
- void Start()
- {
- Destroy(gameObject, DestroyTime);
- }
-
- // Update is called once per frame
- void Update()
- {
- transform.Translate(transform.up * speed * Time.deltaTime);
- }
- }
Enemy01:刀哥
敌人特性:随机出现自毁式攻击。
导入素材:

添加碰撞盒

设置移动脚本:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class Enemy_1 : MonoBehaviour
- {
- public float speed = 7f;
- public float DestroyTime = 10f;
- // Start is called before the first frame update
- void Start()
- {
- Destroy(gameObject, DestroyTime);
- }
-
- // Update is called once per frame
- void Update()
- {
- transform.Translate(-transform.up * speed * Time.deltaTime);
- }
- }
Enemy02:蛋哥
敌人特性:发射子弹。
导入素材:

添加碰撞盒和物理系统

用空子件设置子弹发射位置

制作预制体子弹

挂载脚本:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class bulletMove : MonoBehaviour
- {
- public float speed = 5f;//子弹飞行速度
- public float DestroyTime = 5f;//子弹最大飞行时间
- // Start is called before the first frame update
- void Start()
- {
- Destroy(gameObject, DestroyTime);
- }
-
- // Update is called once per frame
- void Update()
- {
- transform.Translate(transform.up * speed * Time.deltaTime);
- }
- }
设置速度

敌人脚本:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class Enemy_1 : MonoBehaviour
- {
- public float speed = 7f;
- public float DestroyTime = 10f;
- public GameObject bullet;//子弹
- public GameObject gun;
- public int num = 0;//子弹数量
- // Start is called before the first frame update
- void Start()
- {
- Destroy(gameObject, DestroyTime);
- }
-
- // Update is called once per frame
- void Update()
- {
- bullet.transform.position = gun.transform.position;//将角色信息传输给子弹
- if (num < 5)
- {
- num++;
- Instantiate(bullet);//生成子弹预制体
- }
- transform.Translate(-transform.up * speed * Time.deltaTime);
- if (num > 5)
- {
- num--;
- }
- }
- }
挂载组件:

运行:

设置一个GameMassage挂载主要脚本:

随机生成脚本:
public GameObject[] Enemy;//创建预制体数组 float timer = 0;//计时器 Instantiate(Enemy[Random.Range(0, Enemy.Length)], new Vector2(-Random.Range(-8, 10), Random.Range(7, 10)), Quaternion.identity);Enemy[Random.Range(0, Enemy.Length)]:随机生成数量0~数组长度
new Vector2(-Random.Range(-8, 10), Random.Range(7, 10)):随机生成范围x:-8~10,y:7~10
Quaternion.identity:不设置旋转
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class gamemanage : MonoBehaviour
- {
- public GameObject[] Enemy;
- float timer = 0;//计时器
- // Start is called before the first frame update
- void Start()
- {
-
- }
-
- // Update is called once per frame
- void Update()
- {
- timer += Time.deltaTime;
- //三秒生成一个
- if (timer >= 3)
- {
- Instantiate(Enemy[Random.Range(0, Enemy.Length)], new Vector2(-Random.Range(-8, 10), Random.Range(7, 10)), Quaternion.identity);
- timer = 0;//计时器归零
- }
- }
- }
效果:

接下来再给物体添加上碰撞函数就好了:
勾选Trigger

添加碰撞销毁函数
- private void OnTriggerEnter2D(Collider2D collision)
- {
- Destroy(gameObject);
- Destroy(collision.gameObject);
- }
给发射子弹的敌人增加计时器,按一定的时间间隔发射。
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class Enemy_1 : MonoBehaviour
- {
- public float speed = 7f;
- public float DestroyTime = 10f;
- public GameObject bullet;//子弹
- public GameObject gun;
- float timer=0;
- // Start is called before the first frame update
- void Start()
- {
- Destroy(gameObject, DestroyTime);
- }
-
- // Update is called once per frame
- void Update()
- {
- bullet.transform.position = gun.transform.position;//将角色信息传输给子弹
- timer += Time.deltaTime;
- if (timer >= 1)
- {
- Instantiate(bullet);//生成子弹预制体
- timer = 0;
- }
- transform.Translate(-transform.up * speed * Time.deltaTime);
- }
- }
给敌机添加拖尾:


对子弹进行差别性攻击:
我方子弹攻击敌人:
- //敌人的子弹
- private void OnTriggerEnter2D(Collider2D collision)
- {
- if(collision.tag=="Player")
- {
- Destroy(gameObject);
- Destroy(collision.gameObject);
- }
- }
-
- //自己的子弹
- private void OnTriggerEnter2D(Collider2D collision)
- {
- if (collision.tag == "Enemy")
- {
- Destroy(gameObject);
- Destroy(collision.gameObject);
- }
- }
差别性死亡:
靠刚体重力飞行的敌人:
- if (collision.tag == "Enemy")
- {
- //更换爆炸图片
- SpriteRenderer spr = collision.GetComponent
(); - Sprite sp = Resources.Load
("booom"); - spr.sprite = sp;
-
- //停止运动
- collision.GetComponent
().Sleep(); - Destroy(gameObject);
- Destroy(collision.gameObject, diedtime);
- }
靠脚本飞行的敌人:
- public Enemy_1 enemy2;//获取角色移动脚本组件
-
- void Awake()//游戏开始前打开脚本功能
- {
- enemy2.enabled = true;
- }
-
- //碰撞时处理
- if (collision.tag == "Enemy2")
- {
- //更换爆炸图片
- SpriteRenderer spr = collision.GetComponent
(); - Sprite sp = Resources.Load
("booom"); - spr.sprite = sp;
-
- //停止运动
- enemy2.enabled = false;
- Destroy(gameObject);
- Destroy(collision.gameObject, diedtime);
- enemy2.enabled = true;
- }
完整代码:
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class bulletMove : MonoBehaviour
- {
- public float speed = 5f;//子弹飞行速度
- public float DestroyTime = 5f;//子弹最大飞行时间
- float diedtime = 0.2f;
- public Enemy_1 enemy2;//获取角色移动脚本组件
-
- // Start is called before the first frame update
- void Awake()
- {
- enemy2.enabled = true;
- }
-
- void Start()
- {
- Destroy(gameObject, DestroyTime);
- //enemy2.enabled = true;
- }
-
- // Update is called once per frame
- void Update()
- {
- transform.Translate(transform.up * speed * Time.deltaTime);
- }
-
- private void OnTriggerEnter2D(Collider2D collision)
- {
- if (collision.tag == "Enemy")
- {
- //更换爆炸图片
- SpriteRenderer spr = collision.GetComponent
(); - Sprite sp = Resources.Load
("booom"); - spr.sprite = sp;
-
- //停止运动
- collision.GetComponent
().Sleep(); - Destroy(gameObject);
- Destroy(collision.gameObject, diedtime);
- }
- if (collision.tag == "Enemy2")
- {
- //更换爆炸图片
- SpriteRenderer spr = collision.GetComponent
(); - Sprite sp = Resources.Load
("booom"); - spr.sprite = sp;
-
- //停止运动
- enemy2.enabled = false;
- Destroy(gameObject);
- Destroy(collision.gameObject, diedtime);
- enemy2.enabled = true;
- }
- if (collision.tag == "EnemyBullet")
- {//直接销毁
- Destroy(gameObject);
- Destroy(collision.gameObject, diedtime);
- }
- }
- }
敌方子弹打中我方或者敌人撞到我方飞机:
- if(collision.tag=="Player")
- {
-
- //更换爆炸图片
- SpriteRenderer spr = collision.GetComponent
(); - Sprite sp = Resources.Load
("booom"); - spr.sprite = sp;
- Time.timeScale = 0;//时间停止
- }
思绪梳理:
我方飞机撞到敌机:
- if (collision.tag == "Enemy")
- {
- //更换爆炸图片
- SpriteRenderer spr = collision.GetComponent
(); - Sprite sp = Resources.Load
("booom"); - spr.sprite = sp;
- Time.timeScale = 0;
-
- }
我方子弹打中敌机:
- if (collision.tag == "Enemy")//重力机
- {
- //更换爆炸图片
- SpriteRenderer spr = collision.GetComponent
(); - Sprite sp = Resources.Load
("booom"); - spr.sprite = sp;
-
- //停止运动
- collision.GetComponent
().Sleep(); - Destroy(gameObject);
- Destroy(collision.gameObject, diedtime);
- }
- if (collision.tag == "Enemy2")//脚本运动机
- {
- //更换爆炸图片
- SpriteRenderer spr = collision.GetComponent
(); - Sprite sp = Resources.Load
("booom"); - spr.sprite = sp;
-
- //停止运动
- enemy2.enabled = false;
- Destroy(gameObject);
- Destroy(collision.gameObject, diedtime);
- enemy2.enabled = true;
- }
- if (collision.tag == "EnemyBullet")//敌人子弹
- {//直接销毁
- Destroy(gameObject);
- Destroy(collision.gameObject, diedtime);
- }
敌机子弹打中我方:
- if (collision.tag == "Player")
- {
- //更换爆炸图片
- SpriteRenderer spr = collision.GetComponent
(); - Sprite sp = Resources.Load
("booom"); - spr.sprite = sp;
- }
敌人撞机我方:
- if (collision.tag == "Player")
- {
- //更换爆炸图片
- SpriteRenderer spr = collision.GetComponent
(); - Sprite sp = Resources.Load
("booom"); - spr.sprite = sp;
- }
标签整理:
我方子弹

敌方子弹

敌方二号机

敌方一号机

我方机:

死亡重新开始机制:
- using UnityEngine.SceneManagement;
- void Restart()
- {
- SceneManager.LoadScene(SceneManager.GetActiveScene().name);//重新开始本关
- }
- Invoke("Restart", restartDelay);//调用Restart函数,并延时
创建封面

按钮
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
-
- public class BeginGame : MonoBehaviour
- {
- public void LoadNext()
- {
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);//build index
- }
-
- }
项目资源文件汇总:






- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
-
- public class BeginGame : MonoBehaviour
- {
- public void LoadNext()
- {
- SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);//build index
- }
-
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
-
- public class bulletEnemy : MonoBehaviour
- {
- public float speed = 5f;//子弹飞行速度
- public float DestroyTime = 5f;//子弹最大飞行时间
- public float restartDelay = 0.5f;//重新开始延时
- public AudioSource music;//播放音乐
- // Start is called before the first frame update
- void Start()
- {
- Destroy(gameObject, DestroyTime);
- Time.timeScale = 1;
- }
-
- // Update is called once per frame
- void Update()
- {
- transform.Translate(transform.up * speed * Time.deltaTime);
- }
-
- private void OnTriggerEnter2D(Collider2D collision)
- {
- if(collision.tag=="Player")
- {
- //更换爆炸图片
- SpriteRenderer spr = collision.GetComponent
(); - Sprite sp = Resources.Load
("booom"); - spr.sprite = sp;
- music.Play();
- Time.timeScale = 0;//时间停止
- Restart();
- //Invoke("Restart", restartDelay);//调用Restart函数,并延时
- }
- }
- void Restart()
- {
- SceneManager.LoadScene(SceneManager.GetActiveScene().name);//重新开始本关
- Time.timeScale = 1;
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class bulletMove : MonoBehaviour
- {
- public float speed = 5f;//子弹飞行速度
- public float DestroyTime = 5f;//子弹最大飞行时间
- float diedtime = 0.2f;
- public Enemy_1 enemy2;//获取角色移动脚本组件
- public AudioSource music;//播放音乐
-
- // Start is called before the first frame update
- void Awake()
- {
- enemy2.enabled = true;
- }
-
- void Start()
- {
- Destroy(gameObject, DestroyTime);
- //enemy2.enabled = true;
- }
-
- // Update is called once per frame
- void Update()
- {
- transform.Translate(transform.up * speed * Time.deltaTime);
- }
-
- private void OnTriggerEnter2D(Collider2D collision)
- {
- if (collision.tag == "Enemy")//重力机
- {
- //更换爆炸图片
- SpriteRenderer spr = collision.GetComponent
(); - Sprite sp = Resources.Load
("booom"); - spr.sprite = sp;
- music.Play();//爆炸音效
- //停止运动
- collision.GetComponent
().Sleep(); - Destroy(gameObject);
- Destroy(collision.gameObject, diedtime);
- }
- if (collision.tag == "Enemy2")//脚本运动机
- {
- //更换爆炸图片
- SpriteRenderer spr = collision.GetComponent
(); - Sprite sp = Resources.Load
("booom"); - spr.sprite = sp;
- music.Play();//爆炸音效
- //停止运动
- enemy2.enabled = false;
- Destroy(gameObject);
- Destroy(collision.gameObject, diedtime);
- enemy2.enabled = true;
- }
- if (collision.tag == "EnemyBullet")//敌人子弹
- {//直接销毁
- Destroy(gameObject);
- Destroy(collision.gameObject, diedtime);
- }
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
-
- public class Enemy_1 : MonoBehaviour
- {
- public float speed = 5f;
- public float DestroyTime = 10f;
- public GameObject bullet;//子弹
- public GameObject gun;
- float timer=0;
- public float restartDelay = 0.5f;//重新开始延时
- public AudioSource music;//播放音乐
-
-
- // Start is called before the first frame update
- void Start()
- {
- Destroy(gameObject, DestroyTime);
- Time.timeScale = 1;
- }
-
- // Update is called once per frame
- void Update()
- {
- bullet.transform.position = gun.transform.position;//将角色信息传输给子弹
- timer += Time.deltaTime;
- if (timer >= 1)
- {
- Instantiate(bullet);//生成子弹预制体
- timer = 0;
- }
- transform.Translate(-transform.up * speed * Time.deltaTime);
- }
- private void OnTriggerEnter2D(Collider2D collision)
- {
- if (collision.tag == "Player")
- {
- //播放音效
- music.Play();
- //更换爆炸图片
- SpriteRenderer spr = collision.GetComponent
(); - Sprite sp = Resources.Load
("booom"); - spr.sprite = sp;
- Time.timeScale = 0;
- Restart();
- //Invoke("Restart", restartDelay);//调用Restart函数,并延时
- }
- }
- void Restart()
- {
- SceneManager.LoadScene(SceneManager.GetActiveScene().name);//重新开始本关
- Time.timeScale = 1;
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
-
- public class gamemanage : MonoBehaviour
- {
- public GameObject[] Enemy;
- float timer = 0;//计时器
- // Start is called before the first frame update
- void Start()
- {
- Time.timeScale = 1;
- }
-
- // Update is called once per frame
- void Update()
- {
- timer += Time.deltaTime;
- //三秒生成一个
- if (timer >= 1)
- {
- Instantiate(Enemy[Random.Range(0, Enemy.Length)], new Vector2(-Random.Range(-8, 10), Random.Range(7, 10)), Quaternion.identity);
- timer = 0;//计时器归零
- }
- }
- }
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
-
- public class playerController : MonoBehaviour
- {
- public float speed = 5f;//移动速度
- public GameObject bullet;//子弹
- public GameObject gun;
- public int HP = 3;
- public float restartDelay = 0.5f;//重新开始延时
- public AudioSource music;//播放音乐
- // Start is called before the first frame update
- void Start()
- {
- Time.timeScale = 1;
- }
-
- // Update is called once per frame
- void Update()
- {
- float moveX = Input.GetAxisRaw("Horizontal");//控制水平移动方向 A:-1 D:1 不按: 0
- float moveY = Input.GetAxisRaw("Vertical");//控制垂直移动方向 W:-1 S:1 不按: 0
-
- Vector2 position = transform.position;//定义角色位置向量
- position.x += moveX * speed * Time.deltaTime;
- position.y += moveY * speed * Time.deltaTime;
- transform.position = position;//将位置信息传输给角色
-
- bullet.transform.position = gun.transform.position;//将角色信息传输给子弹
-
- if (Input.GetKeyDown(KeyCode.Space))//按下空格键
- {
- Instantiate(bullet);//生成子弹预制体
- }
- }
- private void OnTriggerEnter2D(Collider2D collision)
- {
- if (collision.tag == "Enemy"|| collision.tag == "Enemy2")//撞到敌人1、2和都使对方的爆炸
- {
- //更换爆炸图片
- SpriteRenderer spr = collision.GetComponent
(); - Sprite sp = Resources.Load
("booom"); - spr.sprite = sp;
- music.Play();//爆炸音效
- Time.timeScale = 0;
- Restart();
- //Invoke("Restart", restartDelay);//调用Restart函数,并延时
- }
- }
- void Restart()
- {
- SceneManager.LoadScene(SceneManager.GetActiveScene().name);//重新开始本关
- Time.timeScale = 1;
- }
- }
游戏DEMO演示:
链接:https://pan.baidu.com/s/1G49MmOMYARwi44BjWAXuRw
提取码:h99b