• 2D游戏案例:雷霆战机


    1、制作战机

    导入素材

    添加拖尾特效

     

    添加碰撞盒组件

    给玩家一个player标签:

    用于后续判断碰撞。

    2、控制战机移动

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. ///
    5. /// 控制角色移动、生命、动画等
    6. ///
    7. public class playerController : MonoBehaviour
    8. {
    9. public float speed = 5f;//移动速度
    10. // Start is called before the first frame update
    11. void Start()
    12. {
    13. }
    14. // Update is called once per frame
    15. void Update()
    16. {
    17. float moveX = Input.GetAxisRaw("Horizontal");//控制水平移动方向 A:-1 D:1 不按: 0
    18. float moveY = Input.GetAxisRaw("Vertical");//控制垂直移动方向 W:-1 S:1 不按: 0
    19. Vector2 position = transform.position;//定义角色位置向量
    20. position.x += moveX * speed * Time.deltaTime;
    21. position.y += moveY * speed * Time.deltaTime;
    22. transform.position = position;//将位置信息传输给角色
    23. }
    24. }

    3、制作导弹预制体

    导入素材

    添加拖尾

    添加移动脚本

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class bulletMove : MonoBehaviour
    5. {
    6. public float speed = 5f;//移动速度
    7. // Start is called before the first frame update
    8. void Start()
    9. {
    10. }
    11. // Update is called once per frame
    12. void Update()
    13. {
    14. transform.Translate(transform.up * speed * Time.deltaTime);
    15. }
    16. }

     为子弹添加碰撞盒

    给玩家导弹一个bullet标签:

    用于判断碰撞

     4、战机发射子弹

    按下空格键生成子弹预制体

    飞机脚本:

    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. public class playerController : MonoBehaviour
    4. {
    5. public float speed = 5f;//移动速度
    6. public GameObject bullet;//子弹
    7. // Start is called before the first frame update
    8. void Start()
    9. {
    10. }
    11. // Update is called once per frame
    12. void Update()
    13. {
    14. float moveX = Input.GetAxisRaw("Horizontal");//控制水平移动方向 A:-1 D:1 不按: 0
    15. float moveY = Input.GetAxisRaw("Vertical");//控制垂直移动方向 W:-1 S:1 不按: 0
    16. Vector2 position = transform.position;//定义角色位置向量
    17. position.x += moveX * speed * Time.deltaTime;
    18. position.y += moveY * speed * Time.deltaTime;
    19. transform.position = position;//将位置信息传输给角色
    20. bullet.transform.position = transform.position;//将角色信息传输给子弹
    21. if (Input.GetKeyDown(KeyCode.Space))//按下空格键
    22. {
    23. Instantiate(bullet);//生成子弹预制体
    24. }
    25. }
    26. }

    设置子弹最久飞行时间,过了时间自动销毁预制体

    子弹脚本:
     

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class bulletMove : MonoBehaviour
    5. {
    6. public float speed = 5f;//子弹飞行速度
    7. public float DestroyTime = 5f;//子弹最大飞行时间
    8. // Start is called before the first frame update
    9. void Start()
    10. {
    11. Destroy(gameObject, DestroyTime);
    12. }
    13. // Update is called once per frame
    14. void Update()
    15. {
    16. transform.Translate(transform.up * speed * Time.deltaTime);
    17. }
    18. }

    5、制作敌人

     Enemy01:刀哥

    敌人特性:随机出现自毁式攻击。

    导入素材:

    添加碰撞盒

    设置移动脚本:

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Enemy_1 : MonoBehaviour
    5. {
    6. public float speed = 7f;
    7. public float DestroyTime = 10f;
    8. // Start is called before the first frame update
    9. void Start()
    10. {
    11. Destroy(gameObject, DestroyTime);
    12. }
    13. // Update is called once per frame
    14. void Update()
    15. {
    16. transform.Translate(-transform.up * speed * Time.deltaTime);
    17. }
    18. }

     Enemy02:蛋哥

    敌人特性:发射子弹。

    导入素材:

    添加碰撞盒和物理系统

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

    制作预制体子弹

    挂载脚本:

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class bulletMove : MonoBehaviour
    5. {
    6. public float speed = 5f;//子弹飞行速度
    7. public float DestroyTime = 5f;//子弹最大飞行时间
    8. // Start is called before the first frame update
    9. void Start()
    10. {
    11. Destroy(gameObject, DestroyTime);
    12. }
    13. // Update is called once per frame
    14. void Update()
    15. {
    16. transform.Translate(transform.up * speed * Time.deltaTime);
    17. }
    18. }

    设置速度

     敌人脚本:

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Enemy_1 : MonoBehaviour
    5. {
    6. public float speed = 7f;
    7. public float DestroyTime = 10f;
    8. public GameObject bullet;//子弹
    9. public GameObject gun;
    10. public int num = 0;//子弹数量
    11. // Start is called before the first frame update
    12. void Start()
    13. {
    14. Destroy(gameObject, DestroyTime);
    15. }
    16. // Update is called once per frame
    17. void Update()
    18. {
    19. bullet.transform.position = gun.transform.position;//将角色信息传输给子弹
    20. if (num < 5)
    21. {
    22. num++;
    23. Instantiate(bullet);//生成子弹预制体
    24. }
    25. transform.Translate(-transform.up * speed * Time.deltaTime);
    26. if (num > 5)
    27. {
    28. num--;
    29. }
    30. }
    31. }

     挂载组件:

    运行:

    6、设置随机生成敌人

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

    随机生成脚本:

    1.   public GameObject[] Enemy;//创建预制体数组
    2.     float timer = 0;//计时器
    3. 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:不设置旋转

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class gamemanage : MonoBehaviour
    5. {
    6. public GameObject[] Enemy;
    7. float timer = 0;//计时器
    8. // Start is called before the first frame update
    9. void Start()
    10. {
    11. }
    12. // Update is called once per frame
    13. void Update()
    14. {
    15. timer += Time.deltaTime;
    16. //三秒生成一个
    17. if (timer >= 3)
    18. {
    19. Instantiate(Enemy[Random.Range(0, Enemy.Length)], new Vector2(-Random.Range(-8, 10), Random.Range(7, 10)), Quaternion.identity);
    20. timer = 0;//计时器归零
    21. }
    22. }
    23. }

    效果:

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

     勾选Trigger

     添加碰撞销毁函数

    1. private void OnTriggerEnter2D(Collider2D collision)
    2. {
    3. Destroy(gameObject);
    4. Destroy(collision.gameObject);
    5. }

    7、对游戏的一些优化:

    给发射子弹的敌人增加计时器,按一定的时间间隔发射。

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Enemy_1 : MonoBehaviour
    5. {
    6. public float speed = 7f;
    7. public float DestroyTime = 10f;
    8. public GameObject bullet;//子弹
    9. public GameObject gun;
    10. float timer=0;
    11. // Start is called before the first frame update
    12. void Start()
    13. {
    14. Destroy(gameObject, DestroyTime);
    15. }
    16. // Update is called once per frame
    17. void Update()
    18. {
    19. bullet.transform.position = gun.transform.position;//将角色信息传输给子弹
    20. timer += Time.deltaTime;
    21. if (timer >= 1)
    22. {
    23. Instantiate(bullet);//生成子弹预制体
    24. timer = 0;
    25. }
    26. transform.Translate(-transform.up * speed * Time.deltaTime);
    27. }
    28. }

    给敌机添加拖尾:

     

    对子弹进行差别性攻击:

    我方子弹攻击敌人:

    1. //敌人的子弹
    2. private void OnTriggerEnter2D(Collider2D collision)
    3. {
    4. if(collision.tag=="Player")
    5. {
    6. Destroy(gameObject);
    7. Destroy(collision.gameObject);
    8. }
    9. }
    10. //自己的子弹
    11. private void OnTriggerEnter2D(Collider2D collision)
    12. {
    13. if (collision.tag == "Enemy")
    14. {
    15. Destroy(gameObject);
    16. Destroy(collision.gameObject);
    17. }
    18. }

     差别性死亡:

    靠刚体重力飞行的敌人:
     

    1. if (collision.tag == "Enemy")
    2. {
    3. //更换爆炸图片
    4. SpriteRenderer spr = collision.GetComponent();
    5. Sprite sp = Resources.Load("booom");
    6. spr.sprite = sp;
    7. //停止运动
    8. collision.GetComponent().Sleep();
    9. Destroy(gameObject);
    10. Destroy(collision.gameObject, diedtime);
    11. }

    靠脚本飞行的敌人:

    1. public Enemy_1 enemy2;//获取角色移动脚本组件
    2. void Awake()//游戏开始前打开脚本功能
    3. {
    4. enemy2.enabled = true;
    5. }
    6. //碰撞时处理
    7. if (collision.tag == "Enemy2")
    8. {
    9. //更换爆炸图片
    10. SpriteRenderer spr = collision.GetComponent();
    11. Sprite sp = Resources.Load("booom");
    12. spr.sprite = sp;
    13. //停止运动
    14. enemy2.enabled = false;
    15. Destroy(gameObject);
    16. Destroy(collision.gameObject, diedtime);
    17. enemy2.enabled = true;
    18. }

    完整代码:

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class bulletMove : MonoBehaviour
    5. {
    6. public float speed = 5f;//子弹飞行速度
    7. public float DestroyTime = 5f;//子弹最大飞行时间
    8. float diedtime = 0.2f;
    9. public Enemy_1 enemy2;//获取角色移动脚本组件
    10. // Start is called before the first frame update
    11. void Awake()
    12. {
    13. enemy2.enabled = true;
    14. }
    15. void Start()
    16. {
    17. Destroy(gameObject, DestroyTime);
    18. //enemy2.enabled = true;
    19. }
    20. // Update is called once per frame
    21. void Update()
    22. {
    23. transform.Translate(transform.up * speed * Time.deltaTime);
    24. }
    25. private void OnTriggerEnter2D(Collider2D collision)
    26. {
    27. if (collision.tag == "Enemy")
    28. {
    29. //更换爆炸图片
    30. SpriteRenderer spr = collision.GetComponent();
    31. Sprite sp = Resources.Load("booom");
    32. spr.sprite = sp;
    33. //停止运动
    34. collision.GetComponent().Sleep();
    35. Destroy(gameObject);
    36. Destroy(collision.gameObject, diedtime);
    37. }
    38. if (collision.tag == "Enemy2")
    39. {
    40. //更换爆炸图片
    41. SpriteRenderer spr = collision.GetComponent();
    42. Sprite sp = Resources.Load("booom");
    43. spr.sprite = sp;
    44. //停止运动
    45. enemy2.enabled = false;
    46. Destroy(gameObject);
    47. Destroy(collision.gameObject, diedtime);
    48. enemy2.enabled = true;
    49. }
    50. if (collision.tag == "EnemyBullet")
    51. {//直接销毁
    52. Destroy(gameObject);
    53. Destroy(collision.gameObject, diedtime);
    54. }
    55. }
    56. }

    敌方子弹打中我方或者敌人撞到我方飞机:

    1. if(collision.tag=="Player")
    2. {
    3. //更换爆炸图片
    4. SpriteRenderer spr = collision.GetComponent();
    5. Sprite sp = Resources.Load("booom");
    6. spr.sprite = sp;
    7. Time.timeScale = 0;//时间停止
    8. }

    思绪梳理:

    我方飞机撞到敌机:

    1. if (collision.tag == "Enemy")
    2. {
    3. //更换爆炸图片
    4. SpriteRenderer spr = collision.GetComponent();
    5. Sprite sp = Resources.Load("booom");
    6. spr.sprite = sp;
    7. Time.timeScale = 0;
    8. }

    我方子弹打中敌机:

    1. if (collision.tag == "Enemy")//重力机
    2. {
    3. //更换爆炸图片
    4. SpriteRenderer spr = collision.GetComponent();
    5. Sprite sp = Resources.Load("booom");
    6. spr.sprite = sp;
    7. //停止运动
    8. collision.GetComponent().Sleep();
    9. Destroy(gameObject);
    10. Destroy(collision.gameObject, diedtime);
    11. }
    12. if (collision.tag == "Enemy2")//脚本运动机
    13. {
    14. //更换爆炸图片
    15. SpriteRenderer spr = collision.GetComponent();
    16. Sprite sp = Resources.Load("booom");
    17. spr.sprite = sp;
    18. //停止运动
    19. enemy2.enabled = false;
    20. Destroy(gameObject);
    21. Destroy(collision.gameObject, diedtime);
    22. enemy2.enabled = true;
    23. }
    24. if (collision.tag == "EnemyBullet")//敌人子弹
    25. {//直接销毁
    26. Destroy(gameObject);
    27. Destroy(collision.gameObject, diedtime);
    28. }

     敌机子弹打中我方:

    1. if (collision.tag == "Player")
    2. {
    3. //更换爆炸图片
    4. SpriteRenderer spr = collision.GetComponent();
    5. Sprite sp = Resources.Load("booom");
    6. spr.sprite = sp;
    7. }

    敌人撞机我方:

    1. if (collision.tag == "Player")
    2. {
    3. //更换爆炸图片
    4. SpriteRenderer spr = collision.GetComponent();
    5. Sprite sp = Resources.Load("booom");
    6. spr.sprite = sp;
    7. }

    标签整理:

    我方子弹

    敌方子弹

    敌方二号机

    敌方一号机

    我方机:

     死亡重新开始机制:

    1. using UnityEngine.SceneManagement;
    2. void Restart()
    3. {
    4. SceneManager.LoadScene(SceneManager.GetActiveScene().name);//重新开始本关
    5. }
    6. Invoke("Restart", restartDelay);//调用Restart函数,并延时

    创建封面

     按钮

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. public class BeginGame : MonoBehaviour
    6. {
    7. public void LoadNext()
    8. {
    9. SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);//build index
    10. }
    11. }

    项目资源文件汇总:

     

    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. public class BeginGame : MonoBehaviour
    6. {
    7. public void LoadNext()
    8. {
    9. SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);//build index
    10. }
    11. }
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. public class bulletEnemy : MonoBehaviour
    6. {
    7. public float speed = 5f;//子弹飞行速度
    8. public float DestroyTime = 5f;//子弹最大飞行时间
    9. public float restartDelay = 0.5f;//重新开始延时
    10. public AudioSource music;//播放音乐
    11. // Start is called before the first frame update
    12. void Start()
    13. {
    14. Destroy(gameObject, DestroyTime);
    15. Time.timeScale = 1;
    16. }
    17. // Update is called once per frame
    18. void Update()
    19. {
    20. transform.Translate(transform.up * speed * Time.deltaTime);
    21. }
    22. private void OnTriggerEnter2D(Collider2D collision)
    23. {
    24. if(collision.tag=="Player")
    25. {
    26. //更换爆炸图片
    27. SpriteRenderer spr = collision.GetComponent();
    28. Sprite sp = Resources.Load("booom");
    29. spr.sprite = sp;
    30. music.Play();
    31. Time.timeScale = 0;//时间停止
    32. Restart();
    33. //Invoke("Restart", restartDelay);//调用Restart函数,并延时
    34. }
    35. }
    36. void Restart()
    37. {
    38. SceneManager.LoadScene(SceneManager.GetActiveScene().name);//重新开始本关
    39. Time.timeScale = 1;
    40. }
    41. }
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class bulletMove : MonoBehaviour
    5. {
    6. public float speed = 5f;//子弹飞行速度
    7. public float DestroyTime = 5f;//子弹最大飞行时间
    8. float diedtime = 0.2f;
    9. public Enemy_1 enemy2;//获取角色移动脚本组件
    10. public AudioSource music;//播放音乐
    11. // Start is called before the first frame update
    12. void Awake()
    13. {
    14. enemy2.enabled = true;
    15. }
    16. void Start()
    17. {
    18. Destroy(gameObject, DestroyTime);
    19. //enemy2.enabled = true;
    20. }
    21. // Update is called once per frame
    22. void Update()
    23. {
    24. transform.Translate(transform.up * speed * Time.deltaTime);
    25. }
    26. private void OnTriggerEnter2D(Collider2D collision)
    27. {
    28. if (collision.tag == "Enemy")//重力机
    29. {
    30. //更换爆炸图片
    31. SpriteRenderer spr = collision.GetComponent();
    32. Sprite sp = Resources.Load("booom");
    33. spr.sprite = sp;
    34. music.Play();//爆炸音效
    35. //停止运动
    36. collision.GetComponent().Sleep();
    37. Destroy(gameObject);
    38. Destroy(collision.gameObject, diedtime);
    39. }
    40. if (collision.tag == "Enemy2")//脚本运动机
    41. {
    42. //更换爆炸图片
    43. SpriteRenderer spr = collision.GetComponent();
    44. Sprite sp = Resources.Load("booom");
    45. spr.sprite = sp;
    46. music.Play();//爆炸音效
    47. //停止运动
    48. enemy2.enabled = false;
    49. Destroy(gameObject);
    50. Destroy(collision.gameObject, diedtime);
    51. enemy2.enabled = true;
    52. }
    53. if (collision.tag == "EnemyBullet")//敌人子弹
    54. {//直接销毁
    55. Destroy(gameObject);
    56. Destroy(collision.gameObject, diedtime);
    57. }
    58. }
    59. }
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. public class Enemy_1 : MonoBehaviour
    6. {
    7. public float speed = 5f;
    8. public float DestroyTime = 10f;
    9. public GameObject bullet;//子弹
    10. public GameObject gun;
    11. float timer=0;
    12. public float restartDelay = 0.5f;//重新开始延时
    13. public AudioSource music;//播放音乐
    14. // Start is called before the first frame update
    15. void Start()
    16. {
    17. Destroy(gameObject, DestroyTime);
    18. Time.timeScale = 1;
    19. }
    20. // Update is called once per frame
    21. void Update()
    22. {
    23. bullet.transform.position = gun.transform.position;//将角色信息传输给子弹
    24. timer += Time.deltaTime;
    25. if (timer >= 1)
    26. {
    27. Instantiate(bullet);//生成子弹预制体
    28. timer = 0;
    29. }
    30. transform.Translate(-transform.up * speed * Time.deltaTime);
    31. }
    32. private void OnTriggerEnter2D(Collider2D collision)
    33. {
    34. if (collision.tag == "Player")
    35. {
    36. //播放音效
    37. music.Play();
    38. //更换爆炸图片
    39. SpriteRenderer spr = collision.GetComponent();
    40. Sprite sp = Resources.Load("booom");
    41. spr.sprite = sp;
    42. Time.timeScale = 0;
    43. Restart();
    44. //Invoke("Restart", restartDelay);//调用Restart函数,并延时
    45. }
    46. }
    47. void Restart()
    48. {
    49. SceneManager.LoadScene(SceneManager.GetActiveScene().name);//重新开始本关
    50. Time.timeScale = 1;
    51. }
    52. }
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. public class gamemanage : MonoBehaviour
    6. {
    7. public GameObject[] Enemy;
    8. float timer = 0;//计时器
    9. // Start is called before the first frame update
    10. void Start()
    11. {
    12. Time.timeScale = 1;
    13. }
    14. // Update is called once per frame
    15. void Update()
    16. {
    17. timer += Time.deltaTime;
    18. //三秒生成一个
    19. if (timer >= 1)
    20. {
    21. Instantiate(Enemy[Random.Range(0, Enemy.Length)], new Vector2(-Random.Range(-8, 10), Random.Range(7, 10)), Quaternion.identity);
    22. timer = 0;//计时器归零
    23. }
    24. }
    25. }
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4. public class playerController : MonoBehaviour
    5. {
    6. public float speed = 5f;//移动速度
    7. public GameObject bullet;//子弹
    8. public GameObject gun;
    9. public int HP = 3;
    10. public float restartDelay = 0.5f;//重新开始延时
    11. public AudioSource music;//播放音乐
    12. // Start is called before the first frame update
    13. void Start()
    14. {
    15. Time.timeScale = 1;
    16. }
    17. // Update is called once per frame
    18. void Update()
    19. {
    20. float moveX = Input.GetAxisRaw("Horizontal");//控制水平移动方向 A:-1 D:1 不按: 0
    21. float moveY = Input.GetAxisRaw("Vertical");//控制垂直移动方向 W:-1 S:1 不按: 0
    22. Vector2 position = transform.position;//定义角色位置向量
    23. position.x += moveX * speed * Time.deltaTime;
    24. position.y += moveY * speed * Time.deltaTime;
    25. transform.position = position;//将位置信息传输给角色
    26. bullet.transform.position = gun.transform.position;//将角色信息传输给子弹
    27. if (Input.GetKeyDown(KeyCode.Space))//按下空格键
    28. {
    29. Instantiate(bullet);//生成子弹预制体
    30. }
    31. }
    32. private void OnTriggerEnter2D(Collider2D collision)
    33. {
    34. if (collision.tag == "Enemy"|| collision.tag == "Enemy2")//撞到敌人1、2和都使对方的爆炸
    35. {
    36. //更换爆炸图片
    37. SpriteRenderer spr = collision.GetComponent();
    38. Sprite sp = Resources.Load("booom");
    39. spr.sprite = sp;
    40. music.Play();//爆炸音效
    41. Time.timeScale = 0;
    42. Restart();
    43. //Invoke("Restart", restartDelay);//调用Restart函数,并延时
    44. }
    45. }
    46. void Restart()
    47. {
    48. SceneManager.LoadScene(SceneManager.GetActiveScene().name);//重新开始本关
    49. Time.timeScale = 1;
    50. }
    51. }

     

    游戏DEMO演示:

    链接:https://pan.baidu.com/s/1G49MmOMYARwi44BjWAXuRw 
    提取码:h99b

  • 相关阅读:
    SpringBatch适配不同数据库的两种方法
    尚医通 (十八) --------- EasyExcel 集成
    用Python中的马尔科夫链模拟文本
    SAP接口调用方式总结
    Python二分查找详解
    高德地图热力图问题之重复创建图层
    5. computed 和 watch 的区别?
    【数据结构】自写简易顺序表ArrayList
    知识点快速回顾
    技术的新浪潮:从SOCKS5代理到跨界电商的未来
  • 原文地址:https://blog.csdn.net/qq_51701007/article/details/126182209