1.3.0 - - 见缝插针
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class RotateCirle : MonoBehaviour { 6 7 public float RotateSpeed = 60; 8 9 10 // Update is called once per frame 11 void Update () { 12 transform.Rotate(new Vector3(0, 0, -1) * RotateSpeed * Time.deltaTime); //球旋转 13 } 14 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class PinCircleCollision : MonoBehaviour { 6 7 //检测碰撞触发 8 private void OnTriggerEnter2D(Collider2D collision) 9 { 10 11 if(collision.tag == "PinCircleCollision") 12 { 13 //通过找到GameManager这个物体后调用GameManager下的GameOver()方法 14 15 GameObject.Find("GameManger").GetComponent<GameManger>().GameOver(); 16 } 17 } 18 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class PinMove : MonoBehaviour { 6 7 public float speed = 5; 8 private bool isFly = false; // 针向小球移动 9 private bool isReach = false; // 针由SpawnPoint到达StartPoint 10 private Vector3 TargetPos; // 针正好插在小球上的位置 11 private Transform startPoint; 12 private Transform circlePoint; 13 14 // Use this for initialization 15 void Start () { 16 startPoint = GameObject.Find("StartPoint").transform; //获取针位置 17 circlePoint = GameObject.Find("Circle").transform; //获取球位置 18 TargetPos.y = circlePoint.position.y - 1.7f; //实际计算得出的偏移量1.7 19 } 20 21 // Update is called once per frame 22 void Update () { 23 24 if(isFly==false) 25 { 26 if(isReach==false) 27 { 28 //针由SpawnPoint向StartPoint移动 29 transform.position = Vector3.MoveTowards(transform.position, startPoint.position, speed * Time.deltaTime); 30 //当帧和StartPoint的距离小于一定值时停止移动(优化性能) 31 if(Vector3.Distance(transform.position,startPoint.position)<0.1f) 32 { 33 isReach = true; 34 } 35 } 36 37 } 38 else 39 { 40 //针向球移动 41 transform.position = Vector3.MoveTowards(transform.position, TargetPos, speed * Time.deltaTime); 42 if (Vector3.Distance(transform.position, TargetPos) < 0.1f) 43 { 44 transform.position = TargetPos; //当前位置再传递回给针,确保所有针的位置相同 45 transform.parent = circlePoint; //设置Circle为Pin的父类,针和小球一起旋转 46 isFly = false; 47 } 48 } 49 } 50 51 //控制针向小球移动,由GameManger.cs中鼠标按下时调用 52 public void StatFly() 53 { 54 isFly = true; 55 isReach = true; 56 } 57 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.UI; 5 using UnityEngine.SceneManagement; 6 7 public class GameManger : MonoBehaviour { 8 9 10 private Transform startPoint; 11 private Transform spawnPoint; 12 private PinMove currentPin; 13 private bool isGameOver = false; 14 private int score; 15 private Camera mainCamera; 16 17 public float cameraChangeSpeed = 3; 18 public Text scoreText; 19 public GameObject gameOverText; 20 public Transform pinPrefab; 21 22 23 24 // Use this for initialization 25 void Start () { 26 27 startPoint = GameObject.Find("StartPoint").transform; 28 spawnPoint = GameObject.Find("SpawnPoint").transform; 29 mainCamera = Camera.main; 30 SpawnPin(); 31 } 32 33 // Update is called once per frame 34 void Update () { 35 36 //游戏结束后立即退出,不发射针 37 if (isGameOver) return; 38 39 //按下鼠标左键发射针,并生成新的针 40 if (Input.GetMouseButtonDown(0)) 41 { 42 score++; 43 scoreText.text = score.ToString(); //计分 44 currentPin.StatFly(); 45 SpawnPin(); //当这里没有这个方法生成新的针时,再次点击鼠标后唯一的那个针会错位并自我旋转,应该是transform.parent = circlePoint被多次执行导致 46 } 47 } 48 49 //实例化针 50 void SpawnPin() 51 { 52 currentPin = Instantiate(pinPrefab,spawnPoint.position, pinPrefab.rotation).GetComponent<PinMove>(); 53 } 54 55 //游戏结束 56 //由于两个小球各自的碰撞都会触发GameOver()方法,因此会消耗性能 57 //所以通过这样使GameOver()方法只执行一次 58 public void GameOver() 59 { 60 if (isGameOver) return; 61 gameOverText.SetActive(true); //显示GameOver 62 GameObject.Find("Circle").GetComponent<RotateCirle>().enabled = false; //禁止小球旋转 63 StartCoroutine(GameOverAnimation()); //调用协程 64 isGameOver = true; 65 } 66 67 IEnumerator GameOverAnimation() 68 { 69 while(true) 70 { 71 mainCamera.backgroundColor = Color.Lerp(mainCamera.backgroundColor, Color.red, cameraChangeSpeed*Time.deltaTime); //控制camera背景颜色变化 72 mainCamera.orthographicSize = Mathf.Lerp(mainCamera.orthographicSize, 4, cameraChangeSpeed * Time.deltaTime); //控制camera大小变化 73 //当到达目标值后跳出循环 74 if (mainCamera.orthographicSize - 4 < 0.01f) 75 break; 76 yield return 0; 77 } 78 yield return new WaitForSeconds(1); //暂停1秒 79 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); //重新开始游戏 80 } 81 }