见缝插针
程序管理代码(GameManager)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 using UnityEngine.UI; 5 using UnityEngine.SceneManagement; 6 7 public class GameManager : MonoBehaviour { 8 9 private Transform startPoint; 10 private Transform spawnPoint; 11 private Pin currentPin; 12 private bool isGameOver = false; 13 private int score = 0; 14 private Camera mainCamera; 15 16 public Text scoreText; 17 public GameObject pinPrefab; 18 public float speed = 3; 19 20 21 22 // Use this for initialization 23 void Start () { 24 startPoint = GameObject.Find("StartPoint").transform; 25 spawnPoint = GameObject.Find("SpawnPoint").transform; 26 mainCamera = Camera.main; 27 SpawnPin(); 28 } 29 30 private void Update() 31 { 32 if (isGameOver) return; 33 if (Input.GetMouseButtonDown(0)) 34 { 35 score++; 36 scoreText.text = score.ToString(); 37 currentPin.StartFly(); 38 SpawnPin(); 39 } 40 } 41 42 void SpawnPin() 43 { 44 currentPin = GameObject.Instantiate(pinPrefab, spawnPoint.position, pinPrefab.transform.rotation).GetComponent<Pin>(); 45 } 46 47 public void GameOver() 48 { 49 if (isGameOver) return; 50 GameObject.Find("Circle").GetComponent<RotateSelf>().enabled = false; 51 StartCoroutine(GameOverAnimation()); 52 isGameOver = true; 53 } 54 55 IEnumerator GameOverAnimation() 56 { 57 while (true) 58 { 59 mainCamera.backgroundColor = Color.Lerp(mainCamera.backgroundColor, Color.red, speed * Time.deltaTime); 60 mainCamera.orthographicSize = Mathf.Lerp(mainCamera.orthographicSize, 4, speed * Time.deltaTime); 61 if( Mathf.Abs( mainCamera.orthographicSize-4 )<0.01f) 62 { 63 break; 64 } 65 yield return 0; 66 } 67 yield return new WaitForSeconds(0.2f); 68 SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); 69 } 70 }
针程序(Pin)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class Pin : MonoBehaviour { 6 7 public float speed = 5; 8 private bool isFly = false; 9 private bool isReach = false; 10 private Transform startPoint; 11 12 private Vector3 targetCirclePos; 13 private Transform circle; 14 15 16 // Use this for initialization 17 void Start () { 18 startPoint = GameObject.Find("StartPoint").transform; 19 circle = GameObject.FindGameObjectWithTag("Circle").transform; 20 targetCirclePos = circle.position; 21 targetCirclePos.y -= 1.55f; 22 //circle = GameObject.Find("Circle").transform; 23 } 24 25 // Update is called once per frame 26 void Update () { 27 if (isFly == false) 28 { 29 if (isReach == false) 30 { 31 transform.position = Vector3.MoveTowards(transform.position, startPoint.position, speed * Time.deltaTime); 32 if (Vector3.Distance(transform.position, startPoint.position) < 0.05f) 33 { 34 isReach = true; 35 } 36 } 37 } 38 else 39 { 40 transform.position = Vector3.MoveTowards(transform.position, targetCirclePos, speed * Time.deltaTime); 41 if(Vector3.Distance( transform.position,targetCirclePos) < 0.05f) 42 { 43 transform.position = targetCirclePos; 44 transform.parent = circle; 45 isFly = false; 46 } 47 } 48 } 49 50 public void StartFly() 51 { 52 isFly = true; 53 isReach = true; 54 } 55 }
针头程序(PinHead)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class PinHead : MonoBehaviour { 6 7 private void OnTriggerEnter2D(Collider2D collision) 8 { 9 if (collision.tag == "PinHead") 10 { 11 GameObject.Find("GameManager").GetComponent<GameManager>().GameOver(); 12 } 13 } 14 }
圆盘旋转程序
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class RotateSelf : MonoBehaviour { 6 7 public float speed = 90; 8 9 // Update is called once per frame 10 void Update () { 11 transform.Rotate(new Vector3(0, 0, -speed * Time.deltaTime)); 12 } 13 }