拾蘋果原型學習之後的指令碼筆記記錄
阿新 • • 發佈:2018-11-25
1.拾蘋果原型各個物體的動作
籃筐:隨玩家的滑鼠左右移動,碰到蘋果則接住蘋果
蘋果:下落,如果蘋果碰到地面則消失,並且使其他蘋果一起消失
蘋果樹:左右隨機移動,每隔一定時間落下一個蘋果
2.五個指令碼
Apple;ApplePicker;AppleTree;Basket;HighScore
Apple
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Apple : MonoBehaviour { public static float bottomY = -20f; //宣告一個靜態變數,靜態變數可以被類的所有例項共享。 void Update () { if(transform.position.y < bottomY) { Destroy(this.gameObject); //this呼叫該語句的C#類的當前例項 ApplePicker apScript = Camera.main.GetComponent<ApplePicker>(); apScript.AppleDestroyed(); } } }
AppleTree
using System.Collections; using System.Collections.Generic; using UnityEngine; public class AppleTree : MonoBehaviour { public GameObject applePrefab; //用來初始化蘋果例項的預設 public float speed = 1f;//蘋果樹移動的速度 public float leftAndRightEdge = 10f;//蘋果樹的活動區域,到達邊界時則改變方向 public float chanceToChangeDirection = 0.1f;//蘋果樹改變方向的概率 public float secondsBetweenAppleDrops = 1f; //蘋果出現的時間間隔 // Use this for initialization void Start () { //每秒掉落一個蘋果 InvokeRepeating("DropApple", 2f, secondsBetweenAppleDrops); //InvokeRepeating函式能夠反覆呼叫另一個命名函式 } void DropApple() { GameObject apple = Instantiate(applePrefab) as GameObject; apple.transform.position = transform.position; } // Update is called once per frame void Update () { //基本運動 Vector3 pos = transform.position;//定義了三維向量pos,使其等於蘋果樹當前位置 pos.x += speed * Time.deltaTime; transform.position = pos;//將修改後的pos賦值給transform.position //改變方向,讓蘋果樹在達到leftAndRightEdge值時改變方向 if(pos.x < -leftAndRightEdge) { speed = Mathf.Abs(speed); }else if(pos.x > leftAndRightEdge) { speed = -Mathf.Abs(speed); } } private void FixedUpdate() { //隨機改變運動方向 if(Random.value < chanceToChangeDirection) { speed *= -1;//改變方向 } } }
ApplePicker
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class ApplePicker : MonoBehaviour { public GameObject basketPrefab; public int numBaskets = 3; public float basketBottomY = -14f; public float basketSpacingY = 2f; public List<GameObject> basketList; // Use this for initialization void Start () { basketList = new List<GameObject>(); for(int i = 0; i < numBaskets; i++) { GameObject tBasketGo = Instantiate(basketPrefab) as GameObject; Vector3 pos = Vector3.zero; pos.y = basketBottomY + (basketSpacingY * i); tBasketGo.transform.position = pos; basketList.Add(tBasketGo); } } // Update is called once per frame void Update () { } public void AppleDestroyed() { //消除所有下路中的蘋果 GameObject[] tAppleArray = GameObject.FindGameObjectsWithTag("Apple"); foreach(GameObject tGO in tAppleArray) { Destroy(tGO); } int basketIndex = basketList.Count - 1;//消除一個蘭寬 GameObject tBasketGO = basketList[basketIndex]; basketList.RemoveAt(basketIndex); Destroy(tBasketGO); if(basketList.Count == 0) { SceneManager.LoadScene("_Scene_0"); } } }
Basket
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Basket : MonoBehaviour {
private Text scoreGT;
// Use this for initialization
void Start () {
GameObject scoreGO = GameObject.Find("ScoreCounter");//查詢遊戲物件
scoreGT = scoreGO.GetComponent<Text>();//獲取元件
scoreGT.text = "0";//設定初始分數
}
// Update is called once per frame
void Update () {
Vector3 mousePos2D = Input.mousePosition;
//從Input中獲取滑鼠在螢幕中的當前位置
mousePos2D.z = -Camera.main.transform.position.z;
//攝像機的z座標決定在三維空間中將滑鼠沿z軸向前移動多遠
Vector3 mousePos3D = Camera.main.ScreenToWorldPoint(mousePos2D);
//將該點從二維螢幕空間轉換到三維遊戲世界空間
//將籃筐的x位置移動到滑鼠處的x位置處
Vector3 pos = this.transform.position;
pos.x = mousePos3D.x;
this.transform.position = pos;
}
private void OnCollisionEnter(Collision collision)
{
//檢查與籃筐碰撞的是什麼物件
GameObject collidedWith = collision.gameObject;
//將撞到的遊戲物件賦值給臨時變數collidedWith
if(collidedWith.tag == "Apple")
//檢查collidedWith是否帶有Apple標籤,從而確定是否是Apple物件
//是的話將其銷燬
{
Destroy(collidedWith);
}
int score = int.Parse(scoreGT.text);//將scoreGT轉換為整數值
score += 100;
scoreGT.text = score.ToString();//將分數轉換為字串顯示在螢幕上
if(score > HighScore.score)
{
HighScore.score = score;
}
}
}
HighScore
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HighScore : MonoBehaviour {
static public int score = 0;
// Use this for initialization
void Start () {
}
private void Awake()
{
if (PlayerPrefs.HasKey("ApplePickerHighScore"))
{
score = PlayerPrefs.GetInt("ApplePickerHighScore");
}
PlayerPrefs.SetInt("ApplePickerHighScore", score);
}
// Update is called once per frame
void Update () {
Text gt = this.GetComponent<Text>();
gt.text = "0";
gt.text = "High Score: " + score;
if(score > PlayerPrefs.GetInt("ApplePickerHighScore"))
{
PlayerPrefs.SetInt("ApplePickerHighScore", score);
}
}
}