完成品: Unity3D-Game1
學習目標
物件移動-蛋
複製物件-蛋
鍵盤控制-玩家
碰撞-蛋,玩家
計分
畫面文字-分數
Page 5
改善地方
1. 加入遊戲結束事件和重新遊戲
2. 加入隨機爆炸和死亡物件給玩家作回避
3. 分開PlayerScript.cs畫面文字和建立遊戲介面程式
4. 改善蛋移動順暢(當蛋移動到碰撞玩家時出現”Lag”), 可加入物理現象給蛋;
同時限制蛋只在XY移動.(可到 Game2 參考)
設定:
Bucket
Create -> Cube
Add -> Mesh Collider
Delete -> Box Collider
Add -> PlayerScript.cs
Egg
Add -> rigidbody
untick -> Use Gravity
Delete -> Sphere Collider
Add -> Box Collider
Add -> EggScript.cs
Prefab
|
V
EggPrefab
Create Empty
Reset
Drag -> Prefab
EggCollider
Add -> Cube
untick -> Mesh Renderer
Add -> EggColliderScript.cs
|
V
SpawnObject
Create Empty
Reset
Add -> SpawnerScript.cs
Drag -> EggPrefab
程式碼:
EggScript.cs
using UnityEngine; using System.Collections; public class EggScript : MonoBehaviour { void Awake() { // 增加力量給蛋 //rigidbody.AddForce(new Vector3(0, -100, 0), ForceMode.Force); } // 持續更新蛋移動位置 //Update is called by Unity every frame void Update () { // 蛋在Y方向每秒移動 float fallSpeed = 2 * Time.deltaTime; transform.position -= new Vector3(0, fallSpeed, 0); //蛋在Y方向範圍以外就消失 if (transform.position.y < -1 || transform.position.y >= 20) { //Destroy this gameobject (and all attached components) Destroy(gameObject); } } } // 作者: http://www.m2h.nl/ // 原檔案: https://www.assetstore.unity3d.com/#/content/116(內附教學文件)
SpawnerScript.cs
using UnityEngine; using System.Collections; public class SpawnerScript : MonoBehaviour { // 加入預製物件 public Transform eggPrefab; // 下一次預製物件時間 private float nextEggTime = 0.0f; // 預製物件速度 private float spawnRate = 1.5f; // 更新預製物件位置, 時間, 速度 void Update () { if (nextEggTime < Time.time) { //複製預製物件 SpawnEgg(); nextEggTime = Time.time + spawnRate; // 加速下一次複製預製物件 //Speed up the spawnrate for the next egg spawnRate *= 0.98f; //加速限制在0.3到99這範圍 spawnRate = Mathf.Clamp(spawnRate, 0.3f, 99f); } } //複製預製物件設定 void SpawnEgg() { float addXPos = Random.Range(-1.6f, 1.6f); //限制在X範圍 Vector3 spawnPos = transform.position + new Vector3(addXPos,0,0); //預製物件移動範圍 Instantiate(eggPrefab, spawnPos, Quaternion.identity); //複製預製物件 } } // 作者: http://www.m2h.nl/ // 原檔案: https://www.assetstore.unity3d.com/#/content/116(內附教學文件)
PlayerScript.cs
using UnityEngine; using System.Collections; public class PlayerScript : MonoBehaviour { //設定分數 public int theScore = 0; void Update () { // 玩家水平移動 //These two lines are all there is to the actual movement.. float moveInput = Input.GetAxis("Horizontal") * Time.deltaTime * 3; transform.position += new Vector3(moveInput, 0, 0); // 玩家在X方向範圍移動 //Restrict movement between two values if (transform.position.x <= -2.5f || transform.position.x >= 2.5f) { float xPos = Mathf.Clamp(transform.position.x, -2.5f, 2.5f); //Clamp between min -2.5 and max 2.5 transform.position = new Vector3(xPos, transform.position.y, transform.position.z); } } //OnGUI is called multiple times per frame. Use this for GUI stuff only! void OnGUI() { // 顯示分數 //We display the game GUI from the playerscript //It would be nicer to have a seperate script dedicated to the GUI though... GUILayout.Label("Score: " + theScore); } } // 作者: http://www.m2h.nl/ // 原檔案: https://www.assetstore.unity3d.com/#/content/116(內附教學文件)
EggCollider.cs
using UnityEngine; using System.Collections; public class EggCollider : MonoBehaviour { // 玩家設定 PlayerScript myPlayerScript; //Automatically run when a scene starts void Awake() { // 載入PlayerScript myPlayerScript = transform.parent.GetComponent(); } // 觸發碰撞, 蛋消失, 加分 //Triggered by Unity's Physics void OnTriggerEnter(Collider theCollision) { //In this game we don't need to check *what* we hit; it must be the eggs GameObject collisionGO = theCollision.gameObject; Destroy(collisionGO); myPlayerScript.theScore++; } } // 作者: http://www.m2h.nl/ // 原檔案: https://www.assetstore.unity3d.com/#/content/116(內附教學文件)