完成品: Unity3D-Game4
設定:
Paddle
Add -> Cube
Add -> Paddle.cs
Ball
tag: Player
Add -> Sphere Collider/Material/NoEnergyLoss.physicMaterial
Add -> Rigidbody
Mass: 1
Drag: 0
Angular Drag: 0
untick -> Use Gravity
Add -> Ball.cs
Max. Velocity: 20
Min. Velocity:15
Drag -> BallPrefab
Block
Add -> cube
tag: Pickup
tick -> Box Collider/Is Trigger
Add -> Block.cs
Drag -> Block
|
V
Row
Create Empty
Reset
|
V
Blocks
Create Empty
Reset
Cube
Rotation Y: 270
|
V
Levels
Create Empty
Reset
Rotation Y: 90
Main Camera
tag: MainCamera
Rotation X: 90
Add -> BreakoutGame.cs
BallPrefab
程式碼:
Ball.cs
using UnityEngine; using System.Collections; public class Ball : MonoBehaviour { // 球最大速度 public float maxVelocity = 20; // 球最小速度 public float minVelocity = 15; void Awake () { rigidbody.velocity = new Vector3(0, 0, -18); } void Update () { //Make sure we stay between the MAX and MIN speed. float totalVelocity = Vector3.Magnitude(rigidbody.velocity); if(totalVelocity>maxVelocity){ float tooHard = totalVelocity / maxVelocity; rigidbody.velocity /= tooHard; } else if (totalVelocity < minVelocity) { float tooSlowRate = totalVelocity / minVelocity; rigidbody.velocity /= tooSlowRate; } // 如球在Z超過-3, 就消失. //Is the ball below -3? Then we're game over. if(transform.position.z <= -3){ // 載入BreakoutGame 失敗 BreakoutGame.SP.LostBall(); Destroy(gameObject); } } } // 作者: http://www.m2h.nl/ // 原檔案: https://www.assetstore.unity3d.com/#/content/116(內附教學文件)
Block.cs
using UnityEngine; using System.Collections; public class Block : MonoBehaviour { void OnTriggerEnter () { // 載入BreakoutGame 球擊中磚塊 BreakoutGame.SP.HitBlock(); Destroy(gameObject); } } // 作者: http://www.m2h.nl/ // 原檔案: https://www.assetstore.unity3d.com/#/content/116(內附教學文件)
BreakoutGame.cs
using UnityEngine; using System.Collections; public enum BreakoutGameState { playing, won, lost }; public class BreakoutGame : MonoBehaviour { public static BreakoutGame SP; // 加入 球物件 public Transform ballPrefab; // 磚塊總數量 private int totalBlocks; // 球擊中磚塊分數 private int blocksHit; private BreakoutGameState gameState; void Awake() { SP = this; blocksHit = 0; gameState = BreakoutGameState.playing; totalBlocks = GameObject.FindGameObjectsWithTag("Pickup").Length; Time.timeScale = 1.0f; SpawnBall(); } void SpawnBall() { // 球複製位置 Instantiate(ballPrefab, new Vector3(1.81f, 1.0f , 9.75f), Quaternion.identity); } void OnGUI(){ GUILayout.Space(10); GUILayout.Label(" Hit: " + blocksHit + "/" + totalBlocks); if (gameState == BreakoutGameState.lost) { GUILayout.Label("You Lost!"); if (GUILayout.Button("Try again")) { Application.LoadLevel(Application.loadedLevel); } } else if (gameState == BreakoutGameState.won) { GUILayout.Label("You won!"); if (GUILayout.Button("Play again")) { Application.LoadLevel(Application.loadedLevel); } } } // 球擊中磚塊設定 public void HitBlock() { blocksHit++; //For fun: //Every 10th block will spawn a new ball // 每撞10個磚塊就多一個球 if (blocksHit%10 == 0) { SpawnBall(); } if (blocksHit >= totalBlocks) { WonGame(); } } // 勝利 public void WonGame() { // 遊戲場景播放--停止 Time.timeScale = 0.0f; //Pause game gameState = BreakoutGameState.won; } // 失敗 public void LostBall() { // 找尋遊戲中 Player總數量 int ballsLeft = GameObject.FindGameObjectsWithTag("Player").Length; if(ballsLeft <= 1){ //Was the last ball.. SetGameOver(); } } // 失敗設定 public void SetGameOver() { // 遊戲場景播放--停止 Time.timeScale = 0.0f; //Pause game gameState = BreakoutGameState.lost; } } // 作者: http://www.m2h.nl/ // 原檔案: https://www.assetstore.unity3d.com/#/content/116(內附教學文件)
Paddle.cs
using UnityEngine; using System.Collections; public class Paddle : MonoBehaviour { // 玩家速度設定 public float moveSpeed = 15; void Update () { // 玩家水平移動 float moveInput = Input.GetAxis("Horizontal") * Time.deltaTime * moveSpeed; transform.position += new Vector3(moveInput, 0, 0); // 玩家移動最大範圍 float max = 14.0f; if (transform.position.x <= -max || transform.position.x >= max) { float xPos = Mathf.Clamp(transform.position.x, -max, max); //Clamp between min -5 and max 5 transform.position = new Vector3(xPos, transform.position.y, transform.position.z); } } // 玩家碰撞球的反應 void OnCollisionExit(Collision collisionInfo ) { //Add X velocity..otherwise the ball would only go up&down Rigidbody rigid = collisionInfo.rigidbody; float xDistance = rigid.position.x - transform.position.x; rigid.velocity = new Vector3(rigid.velocity.x + xDistance/2, rigid.velocity.y, rigid.velocity.z); } } // 作者: http://www.m2h.nl/ // 原檔案: https://www.assetstore.unity3d.com/#/content/116(內附教學文件)