Unity3D-Game3

game3

完成品: Unity3D-Game3

設定:
Marble
Add -> Sphere
Add -> Rigidbody
Add -> MarbleControl.cs
MovementSpeed: 10

Straight
Add -> Cube
Scale:
|
V
Levels
Create Empty
Reset

GameoverTrigger
Add -> Cube
tick -> Box Collider/Is Trigger
Add -> GameoverTrigger.cs

Gem
tag:Pickup
Add -> Mesh Collider
tick -> Is Trigger/Convex/smooth sphere collision
Add -> Rigidbody
untick -> Use Gravity
|
V
Gems
Create Empty
Reset

Main Camera
tag: MainCamera
Add -> MarbleCamera.cs
Target: Marble
RelativeHeight:
ZDistance:
DampSpeed:
Add -> MarbleGameManager.cs

程式碼:
MarbleGameManager.cs

 
using UnityEngine;
using System.Collections;

// 定義常數 -- 正常, 勝利, 失敗
public enum MarbleGameState {playing, won, lost };

public class MarbleGameManager : MonoBehaviour
{
    public static MarbleGameManager SP;

    private int totalGems;
    private int foundGems;
    private MarbleGameState gameState;


    void Awake()
    {
        SP = this; 
        foundGems = 0;
        gameState = MarbleGameState.playing;
		// 取得Pickup總數量
        totalGems = GameObject.FindGameObjectsWithTag("Pickup").Length;
        // 遊戲場景播放--正常
		Time.timeScale = 1.0f;
    }

	void OnGUI () {
	    GUILayout.Label(" Found gems: "+foundGems+"/"+totalGems );

        if (gameState == MarbleGameState.lost)
        {
            GUILayout.Label("You Lost!");
            if(GUILayout.Button("Try again") ){
                Application.LoadLevel(Application.loadedLevel);
            }
        }
        else if (gameState == MarbleGameState.won)
        {
            GUILayout.Label("You won!");
            if(GUILayout.Button("Play again") ){
                Application.LoadLevel(Application.loadedLevel);
            }
        }
	}
	// 勝利條件
    public void FoundGem()
    {
        foundGems++;
        if (foundGems >= totalGems)
        {
            WonGame();
        }
    }
	// 勝利
    public void WonGame()
    {
        // 遊戲場景播放--停止
		Time.timeScale = 0.0f; //Pause game
        gameState = MarbleGameState.won;
    }
	// 失敗
    public void SetGameOver()
    {
        // 遊戲場景播放--停止
		Time.timeScale = 0.0f; //Pause game
        gameState = MarbleGameState.lost;
    }
}

// 作者: http://www.m2h.nl/
// 原檔案: https://www.assetstore.unity3d.com/#/content/116(內附教學文件)

MarbleControl.cs

 
using UnityEngine;
using System.Collections;

public class MarbleControl : MonoBehaviour {

    // 玩家速度設定
	public float movementSpeed = 6.0f;
	
	void Update () {
			// 玩家移動
        	Vector3 movement = (Input.GetAxis("Horizontal") * -Vector3.left * movementSpeed) + (Input.GetAxis("Vertical") * Vector3.forward *movementSpeed);
			// 不斷加速
			movement *= Time.deltaTime;
        	rigidbody.AddForce(movement, ForceMode.Force);
	}

    void OnTriggerEnter  (Collider other  ) {
		// 如碰撞Pickup
        if (other.tag == "Pickup")
        {
            // 載入 MarbleGameManager 勝利條件
			MarbleGameManager.SP.FoundGem();
            // 目標物消失
			Destroy(other.gameObject);
        }
        else
        {
            //Other collider.. See other.tag and other.name
        }        
    }
}

// 作者: http://www.m2h.nl/
// 原檔案: https://www.assetstore.unity3d.com/#/content/116(內附教學文件)

MarbleCamera.cs

 
using UnityEngine;
using System.Collections;

public class MarbleCamera : MonoBehaviour {
	
	// 加入目標物
    public Transform target;
	// 攝影機高度
	public float relativeHeigth = 10.0f;
	// 攝影機水平
    public float zDistance = 5.0f;
	// 攝影機和目標物距離
    public float dampSpeed = 2;

    void Update () {
		// 攝影機新位置 = 目標物 + 攝影機設定
        Vector3 newPos = target.position + new Vector3(0, relativeHeigth, -zDistance);
		// 攝影機位置
        transform.position = Vector3.Lerp(transform.position, newPos, Time.deltaTime*dampSpeed);
	}
}

// 作者: http://www.m2h.nl/
// 原檔案: https://www.assetstore.unity3d.com/#/content/116(內附教學文件)

GameoverTrigger.cs

 
using UnityEngine;
using System.Collections;

public class GameoverTrigger : MonoBehaviour {

    void OnTriggerEnter()
    {
        // 載入MarbleGameManager 失敗
		MarbleGameManager.SP.SetGameOver();
    }
}

// 作者: http://www.m2h.nl/
// 原檔案: https://www.assetstore.unity3d.com/#/content/116(內附教學文件)

發佈留言