using UnityEngine; //引入unity的函數定義 using System.Collections; public class score : MonoBehaviour { //繼承 int mScore=0; float vSbarValue =0.0f; void Start () { //遊戲一開始會呼叫的函數 GetComponent().velocity = new Vector3(0, 0, 0); GetComponent().isKinematic = true; } int t_valueUpDown=0; void Update () { //遊戲執行時會呼叫的函數 float translation = Time.deltaTime * 20; if(vSbarValue>9.0f){t_valueUpDown=1; } if(vSbarValue<0.0f){t_valueUpDown=0;} if(t_valueUpDown==0){ vSbarValue=vSbarValue+translation; }else{ vSbarValue=vSbarValue-translation; } if (Input.GetButtonDown("Fire1")) { GetComponent().isKinematic = false; //AddForce() 是施加外力給物體 GetComponent().AddForce (Vector3.up * (vSbarValue*120) ); //60 //往上 GetComponent().AddForce (Vector3.forward * 180); //往前 GetComponent().AddForce (Vector3.right* 2); //往右 } if(gameObject.gameObject.transform.position.y<-10){ GetComponent().velocity = new Vector3(0, 0, 0); GetComponent().isKinematic = true; GetComponent().position=new Vector3(0,-0.83f,-6.56f); } } void OnTriggerExit(Collider other) { //OnTriggerExit碰撞觸發的事件 if(other.transform.name=="PointEvent"){ mScore=mScore+1; } } void OnGUI () { //顯示分數,文字 GUI.VerticalScrollbar(new Rect(25, 20, 100, 120), vSbarValue, 1.0F, 10.0F, 0.0F); GUI.Label(new Rect(0,0,400,400)," Score:"+mScore); } }