using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class script1 : MonoBehaviour { float speed1=15.0f; Rigidbody2D ufo1; int num; Text txtnum; Text txtwin; // Start is called before the first frame update void Start() { ufo1 = GameObject.Find("UFO").GetComponent(); num = 12; txtnum = GameObject.Find("Num").GetComponent(); txtwin = GameObject.Find("Win").GetComponent(); } // Update is called once per frame void Update() { if(Input.GetKey("up") || Input.GetKey("down") || Input.GetKey("left") || Input.GetKey("right")) { float x1 = Input.GetAxis("Horizontal"); float y1 = Input.GetAxis("Vertical"); Vector2 movement = new Vector2(x1,y1); ufo1.AddForce(movement * speed1); //ufo1.velocity = movement * speed1; } else { ufo1.velocity = new Vector2(0,0); } } void OnCollisionEnter2D(Collision2D object1) { if(object1.gameObject.CompareTag("Pickup")) { Destroy(object1.gameObject); num --; txtnum.text = "Num = " + num; if(num==0) { txtwin.text = "你獲勝"; } } } }