当前位置:   article > 正文

Unity代码基础第五天之“见缝插针”游戏(零基础)_unity2d游戏见缝插针资源包

unity2d游戏见缝插针资源包

一、创建场景、导入资源

1.转为2D模式

2.调整一下摄像头的背景颜色,以及改成Solid Color

3.导入资源包image,这个如果需要,可以私我

 

二、开发旋转的小球和分数

1.调整Circle的大小

2.在UI里新建一个text,调整好颜色,居中,调整Canvas的大小、宽高等

3.摄像头Main Camera要拖拽到Canvas的Event camera

三、控制小球旋转

1.给小球写一段代码,拖拽给小球

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Rotatefun : MonoBehaviour
  5. {
  6. public float speed = 90;
  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.Rotate(new Vector3(0,0,-speed * Time.deltaTime));//做一个旋转,每秒1米,加个负号就是顺时针旋转
  15. }
  16. }

 

四、开发针的预制体

1.用资源库里的pin做一个针,调整大小长度,Z轴调成90度

2.用资源库的Circle做一个针头,调整大小,并添加碰撞器Cricle collider 2D

3.最后把整个pin拖拽到预制体文件夹里面

五、开发GameManager去生成针

1.新建3个空的游戏物体StartPoint和SpawnPoint还有GameManager这3个,前面2个沿一条直线排序,第三个位置rest重置下,如图

2.将预制体pin拖拽到游戏物体定义的公有变量下

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GameManager : MonoBehaviour
  5. {
  6. public Transform startpoint;//定义一个Transform类型的对象
  7. public Transform spawnpoint;
  8. public GameObject pinPrefab;
  9. // Start is called before the first frame update
  10. void Start()
  11. {
  12. startpoint = GameObject.Find("StartPoint").transform;//找到StartPoint对象的transform属性
  13. spawnpoint = GameObject.Find("SpawnPoint ").transform;
  14. GOspan();
  15. }
  16. void GOspan() {
  17. GameObject.Instantiate(pinPrefab, spawnpoint.position, pinPrefab.transform.rotation);//实例化 物体 位置 旋转位置
  18. }
  19. // Update is called once per frame
  20. void Update()
  21. {
  22. }
  23. }

六、控制针移动到就位位置

1.新建一个脚本拖拽给预制体Pin

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Pin : MonoBehaviour
  5. {
  6. private bool isFly = false;
  7. private bool isReach = false;
  8. private Transform startPoint;
  9. public float speed = 5;
  10. // Start is called before the first frame update
  11. void Start()
  12. {
  13. startPoint = GameObject.Find("StartPoint").transform;
  14. }
  15. // Update is called once per frame
  16. void Update()
  17. {
  18. if(isFly == false){
  19. if (isReach == false)
  20. {
  21. transform.position = Vector3.MoveTowards(transform.position, startPoint.position,speed*Time.deltaTime);//(transform.position)针 运动的方法-当前的位置-目标的位置-运动的距离
  22. //判断是否到达目标位置
  23. if (Vector3.Distance(transform.position, startPoint.position) < 0.05f) {//如果当前位置-和-目标位置,到达了0.05的时候
  24. isReach = true;//性能优化
  25. }
  26. }
  27. }
  28. }
  29. }

七、控制针的插入

1.在脚本GameManager 中添加,点击事件,等其他方法

2.在脚本Pin中,添加StartFly()开始方法,编写针的运动

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GameManager : MonoBehaviour
  5. {
  6. public Transform startpoint;//定义一个Transform类型的对象
  7. public Transform spawnpoint;
  8. public Pin currentPin;//定义一个类型为Pin的对象
  9. public GameObject pinPrefab;
  10. // Start is called before the first frame update
  11. void Start()
  12. {
  13. startpoint = GameObject.Find("StartPoint").transform;//找到StartPoint对象的transform属性
  14. spawnpoint = GameObject.Find("SpawnPoint ").transform;
  15. GOspan();
  16. }
  17. void GOspan() {
  18. //GameObject.Instantiate(pinPrefab, spawnpoint.position, pinPrefab.transform.rotation);//实例化 物体 位置 旋转位置
  19. currentPin = GameObject.Instantiate(pinPrefab, spawnpoint.position, pinPrefab.transform.rotation).GetComponent<Pin>();//转为了Pin类型,可以在Pin类中访问
  20. }
  21. // Update is called once per frame
  22. void Update()
  23. {
  24. if (Input.GetMouseButtonDown(0)) {//如果按下鼠标左键
  25. currentPin.StartFly();
  26. }
  27. }
  28. }
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Pin : MonoBehaviour//飞行的类
  5. {
  6. private bool isFly = false;
  7. private bool isReach = false;
  8. private Transform startPoint;
  9. private Transform circle;
  10. public float speed = 5;
  11. // Start is called before the first frame update
  12. void Start()
  13. {
  14. startPoint = GameObject.Find("StartPoint").transform;
  15. //circle = GameObject.Find("Circle").transform;//全局查找
  16. circle = GameObject.FindGameObjectWithTag("Cirdea").transform;//通过tag标签获取
  17. //Find("Cirdea").transform;
  18. }
  19. // Update is called once per frame
  20. void Update()
  21. {
  22. if (isFly == false)
  23. {
  24. if (isReach == false)
  25. {
  26. transform.position = Vector3.MoveTowards(transform.position, startPoint.position, speed * Time.deltaTime);//(transform.position)针 运动的方法-当前的位置-目标的位置-运动的距离
  27. //判断是否到达目标位置
  28. if (Vector3.Distance(transform.position, startPoint.position) < 0.05f)
  29. {//如果当前位置-和-目标位置,到达了0.05的时候
  30. isReach = true;//性能优化
  31. }
  32. }
  33. }
  34. else {
  35. transform.position = Vector3.MoveTowards(transform.position, circle.position, speed * Time.deltaTime);//针的运动 = 当前位置-目标位置-运动的距离
  36. }
  37. }
  38. public void StartFly() {//方法要设置为public,因为GaneManager中要用到
  39. isFly = true;
  40. isReach = true;
  41. }
  42. }

八、控制针达到的位置和针的连环发射

1.把预制体放置到针插中圆的最合适位置,然后用圆的Y-针的Y得到1.44f,也就是“控制针达到的位置”。

2.如果小于0.05f判断是到达了目标位置后,进行一个针的跟随。isFly = false;//这样就不会执行所有代码,针就不会运动了,插在了圆上面

3.在GameManager类中,在按下事件中,调用实列的方法就可以做到连环发射,每点一次就实列化一次。如图

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Pin : MonoBehaviour//飞行的类
  5. {
  6. private bool isFly = false;
  7. private bool isReach = false;
  8. private Transform startPoint;
  9. private Transform circle;
  10. public float speed = 5;
  11. private Vector3 targetCriclePos;
  12. // Start is called before the first frame update
  13. void Start()
  14. {
  15. startPoint = GameObject.Find("StartPoint").transform;
  16. //circle = GameObject.Find("Circle").transform;//全局查找
  17. circle = GameObject.FindGameObjectWithTag("Cirdea").transform;//通过tag标签获取
  18. targetCriclePos = circle.position;
  19. targetCriclePos.y -= 1.44f;//把预制体放置到针插中圆的最合适位置,然后用圆的Y-针的Y得到1.44f
  20. }
  21. // Update is called once per frame
  22. void Update()
  23. {
  24. if (isFly == false)
  25. {
  26. if (isReach == false)
  27. {
  28. transform.position = Vector3.MoveTowards(transform.position, startPoint.position, speed * Time.deltaTime);//(transform.position)针 运动的方法-当前的位置-目标的位置-运动的距离
  29. //判断是否到达目标位置
  30. if (Vector3.Distance(transform.position, startPoint.position) < 0.05f)
  31. {//如果当前位置-和-目标位置,到达了0.05的时候目标位置
  32. isReach = true;//性能优化
  33. }
  34. }
  35. }
  36. else {
  37. //transform.position = Vector3.MoveTowards(transform.position, circle.position, speed * Time.deltaTime);//针的运动 = 当前位置-目标位置-运动的距离
  38. transform.position = Vector3.MoveTowards(transform.position, targetCriclePos, speed * Time.deltaTime);//targetCriclePos替换目标位置
  39. if (Vector3.Distance(transform.position, targetCriclePos) <0.05f){//如果小于0.05f判断是到达了目标位置
  40. transform.position = targetCriclePos;//把当前的位置 = 目标位置
  41. transform.parent = circle;//跟随小球运动
  42. isFly = false;//这样就不会执行所有代码,针就不会运动了,插在了圆上面
  43. }
  44. }
  45. }
  46. public void StartFly() {//方法要设置为public,因为GaneManager中要用到
  47. isFly = true;
  48. isReach = true;
  49. }
  50. }

 

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class GameManager : MonoBehaviour
  5. {
  6. public Transform startpoint;//定义一个Transform类型的对象
  7. public Transform spawnpoint;
  8. public Pin currentPin;//定义一个类型为Pin的对象
  9. public GameObject pinPrefab;
  10. // Start is called before the first frame update
  11. void Start()
  12. {
  13. startpoint = GameObject.Find("StartPoint").transform;//找到StartPoint对象的transform属性
  14. spawnpoint = GameObject.Find("SpawnPoint ").transform;
  15. GOspan();
  16. }
  17. void GOspan() {
  18. //GameObject.Instantiate(pinPrefab, spawnpoint.position, pinPrefab.transform.rotation);//实例化 物体 位置 旋转位置
  19. currentPin = GameObject.Instantiate(pinPrefab, spawnpoint.position, pinPrefab.transform.rotation).GetComponent<Pin>();//转为了Pin类型,可以在Pin类中访问
  20. }
  21. // Update is called once per frame
  22. void Update()
  23. {
  24. if (Input.GetMouseButtonDown(0)) {//如果按下鼠标左键
  25. currentPin.StartFly();
  26. GOspan();//连环发射,每点一次就实列化一次
  27. }
  28. }
  29. }

九、针头的碰撞和游戏结束的处理

1.在预制体中,对针头添加一个组件“Rigidbody 2D”并把重力调为0,不然会往下掉

2.使用触发模式,不然private void OnTriggerEnter2D(Collider2D collision)方法不会生效

3.添加新的脚本Over 给针头,在GameManager 类里面写下新的方法

 

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Over : MonoBehaviour
  5. {
  6. private void OnTriggerEnter2D(Collider2D collision)//注意:要开启在Circle Collider 2D组件中的IS Trigger,不然不会触发,非常重要
  7. {
  8. if (collision.tag == "PinHead") {
  9. Debug.Log(123);
  10. GameObject.Find("GameManager").GetComponent<GameManager>().Gameover();//在GameManager类中,找到结束方法
  11. }
  12. }
  13. // Start is called before the first frame update
  14. void Start()
  15. {
  16. }
  17. // Update is called once per frame
  18. void Update()
  19. {
  20. }
  21. }
  22. using System.Collections;
  23. using System.Collections.Generic;
  24. using UnityEngine;
  25. public class GameManager : MonoBehaviour
  26. {
  27. public Transform startpoint;//定义一个Transform类型的对象
  28. public Transform spawnpoint;
  29. public Pin currentPin;//定义一个类型为Pin的对象
  30. public GameObject pinPrefab;
  31. private bool isGameOver;
  32. void Start()
  33. {
  34. startpoint = GameObject.Find("StartPoint").transform;//找到StartPoint对象的transform属性
  35. spawnpoint = GameObject.Find("SpawnPoint ").transform;
  36. GOspan();
  37. }
  38. void GOspan() {
  39. //GameObject.Instantiate(pinPrefab, spawnpoint.position, pinPrefab.transform.rotation);//实例化 物体 位置 旋转位置
  40. currentPin = GameObject.Instantiate(pinPrefab, spawnpoint.position, pinPrefab.transform.rotation).GetComponent<Pin>();//转为了Pin类型,可以在Pin类中访问
  41. }
  42. // Update is called once per frame
  43. void Update()
  44. {
  45. if (isGameOver) return;
  46. if (Input.GetMouseButtonDown(0)) {//如果按下鼠标左键
  47. currentPin.StartFly();
  48. GOspan();//连环发射,每点一次就实列化一次
  49. }
  50. }
  51. public void Gameover() {
  52. if (isGameOver) return;
  53. GameObject.Find("Circle").GetComponent<Rotatefun>().enabled = false;//圆停止旋转
  54. isGameOver = true;
  55. }
  56. }

十、添加分数显示

1.在脚本中创建一个Text类型的对象,在舞台上面把文本拖拽到这个text上面

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class GameManager : MonoBehaviour
  6. {
  7. public Transform startpoint;//定义一个Transform类型的对象
  8. public Transform spawnpoint;
  9. public Pin currentPin;//定义一个类型为Pin的对象
  10. public GameObject pinPrefab;
  11. private bool isGameOver;
  12. public Text text;
  13. int Score = 0;
  14. void Start()
  15. {
  16. startpoint = GameObject.Find("StartPoint").transform;//找到StartPoint对象的transform属性
  17. spawnpoint = GameObject.Find("SpawnPoint ").transform;
  18. GOspan();
  19. }
  20. void GOspan() {
  21. //GameObject.Instantiate(pinPrefab, spawnpoint.position, pinPrefab.transform.rotation);//实例化 物体 位置 旋转位置
  22. currentPin = GameObject.Instantiate(pinPrefab, spawnpoint.position, pinPrefab.transform.rotation).GetComponent<Pin>();//转为了Pin类型,可以在Pin类中访问
  23. }
  24. // Update is called once per frame
  25. void Update()
  26. {
  27. if (isGameOver) return;
  28. if (Input.GetMouseButtonDown(0)) {//如果按下鼠标左键
  29. currentPin.StartFly();
  30. GOspan();//连环发射,每点一次就实列化一次
  31. Score++;
  32. text.text = Score.ToString();//把一个 Number 对象转换为一个字符串
  33. }
  34. }
  35. public void Gameover() {
  36. if (isGameOver) return;//因为每个针头都有,Gameover方法,所以要这样进行处理,只执行一次
  37. GameObject.Find("Circle").GetComponent<Rotatefun>().enabled = false;//圆停止旋转
  38. isGameOver = true;
  39. }
  40. }

十一、游戏结束动画的显示

1.利用协程写一个无限循环,改变背景颜色和大小,暂时0.2秒,再重新加载场景

2.注意摄像头中的Projection属性是Orthographic,

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6. public class GameManager : MonoBehaviour
  7. {
  8. public Transform startpoint;//定义一个Transform类型的对象
  9. public Transform spawnpoint;
  10. public Pin currentPin;//定义一个类型为Pin的对象
  11. public GameObject pinPrefab;
  12. private bool isGameOver;
  13. public Text text;
  14. int Score = 0;
  15. public Camera mainCamera;
  16. public float speed = 3;
  17. void Start()
  18. {
  19. startpoint = GameObject.Find("StartPoint").transform;//找到StartPoint对象的transform属性
  20. spawnpoint = GameObject.Find("SpawnPoint ").transform;
  21. GOspan();
  22. }
  23. void GOspan() {
  24. //GameObject.Instantiate(pinPrefab, spawnpoint.position, pinPrefab.transform.rotation);//实例化 物体 位置 旋转位置
  25. currentPin = GameObject.Instantiate(pinPrefab, spawnpoint.position, pinPrefab.transform.rotation).GetComponent<Pin>();//转为了Pin类型,可以在Pin类中访问
  26. }
  27. // Update is called once per frame
  28. void Update()
  29. {
  30. if (isGameOver) return;
  31. if (Input.GetMouseButtonDown(0)) {//如果按下鼠标左键
  32. currentPin.StartFly();
  33. GOspan();//连环发射,每点一次就实列化一次
  34. Score++;
  35. text.text = Score.ToString();//把一个 Number 对象转换为一个字符串//必须引用using UnityEngine.UI;
  36. }
  37. }
  38. public void Gameover() {
  39. if (isGameOver) return;//因为每个针头都有,Gameover方法,所以要这样进行处理,只执行一次
  40. GameObject.Find("Circle").GetComponent<Rotatefun>().enabled = false;//圆停止旋转
  41. StartCoroutine(GameOverAnimation());//引用协程
  42. isGameOver = true;
  43. }
  44. IEnumerator GameOverAnimation() {//利用协程
  45. while (true) {
  46. mainCamera.backgroundColor = Color.Lerp(mainCamera.backgroundColor,Color.red,speed*Time.deltaTime);//背景颜色 = 当前颜色-目标颜色-速度
  47. mainCamera.orthographicSize = Mathf.Lerp(mainCamera.orthographicSize, 4, speed * Time.deltaTime);//大小=当前大小-目标颜色-速度
  48. if (Mathf.Abs(mainCamera.orthographicSize-4)<0.01f) {//当前大小与目前大小的一个差值
  49. break;//跳出循环
  50. }
  51. yield return 0;//1次暂停1针
  52. }
  53. yield return new WaitForSeconds(0.2f);//跳出循环后,暂停0.2
  54. SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);//重新加载当前场景,必须引用using UnityEngine.SceneManagement;-当前场景的buildIndex
  55. }
  56. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/47862
推荐阅读
相关标签
  

闽ICP备14008679号