# prg 5-8 箭頭控制並顯示次數在畫面上 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ArrowController : MonoBehaviour { // Start is called before the first frame update GameObject player; GameObject steak; void Start() { this.player = GameObject.Find("player"); steak = GameObject.Find("Steak_txt"); } // 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(); ArrowGenerator.eat_steak += 1; steak.GetComponent().text = "吃了幾塊 "+ArrowGenerator.eat_steak.ToString("D5") + "牛排"; Destroy(gameObject); } } }