# prg 5-8 箭頭控制並顯示次數在畫面上 using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; public class ArrowController : MonoBehaviour { GameObject player; GameObject arrow_text; public int arrows = 0; public TextMeshProUGUI textMeshProUGUI; // Start is called before the first frame update void Start() { this.player = GameObject.Find("player"); arrow_text = GameObject.Find("Arrow_text"); } // Update is called once per frame void Update() { transform.Translate(0,-0.1f,0); if (transform.position.y < -5.0f) { Destroy(gameObject); } Vector2 p1 = transform.position; Vector2 p2 = this.player.transform.position; Vector2 dir = p1 - p2; float d = dir.magnitude; float r1 = 0.5f; float r2 = 1.0f; if (d < r1+r2) { GameObject director = GameObject.Find("GameDirector"); director.GetComponent().DecreaseHp(); Destroy(gameObject); arrows = arrows+1; arrow_text.GetComponent().text = "Eat Arrow : "+arrows.ToString("D3"); //Destroy(player); } } }