文章目錄
- 前言
- 一、實驗目的
- 二、實驗環境
- 三、實驗內容
- 1.創建游戲世界
- 1.1雕刻游戲世界
- 1.2添加環境
- 2.角色控制器
- 3.添加游戲控制物件
- 4.添加腳本和把腳本連接在一起
- 5.測驗游戲
- 總結
前言
游戲開發引擎 實驗1:復雜地形穿越
一、實驗目的
- 怎樣設計基本的游戲;
- 怎樣應用地形知識構建特定于游戲的世界;
- 怎樣向游戲中添加物件以提供互動性;
- 怎樣測驗和調整完成的游戲,
??在本實驗中,你將消化迄今為止所學的知識,并使用它們構建你的第一個 Unity 游戲, 你首先將了解游戲的基本設計元素,接著,將構建游戲發生的世界,然后,將添加一些互動 性物件,以使玩家能夠玩游戲,最后,將開始玩游戲,并執行任何必要的調整以改進體驗,
二、實驗環境
- 作業系統:WINDOWS 10
- 開發工具:UNITY
- 實驗設備:PC
三、實驗內容
??自己搭建一個有障礙或陷阱等元素的地形,有玩家在地形中穿越的出發點及終點,在穿越程序中碰到障礙或掉入陷阱則視為穿越失敗一次,玩家自動回到出發點重新開始穿越,失敗次數超過某個設定值或到達終點,都結束一次游戲程序,相應顯示成功或失敗的相關統計資訊(如用時、失敗次數等式)并可重新開始新的一次游戲,玩家可以是第一人稱或第三人稱的角色控制器,加入必要游戲物件、模型及撰寫有關腳本等,能通過鍵盤和滑鼠相結合進行穿越,可根據需要增加其他內容,
1.創建游戲世界

1.1雕刻游戲世界
(1)在名為 Amazing Racer 的檔案夾中創建一個新專案,并向該專案中添加一個地形,
(2)把該地形的解析度設定為 200(寬)×100(長)×100(高)(在 Terrain Settings 的
Resolution 區域中設定它),
(3)匯入terrain.raw檔案作為地形的高度圖(通過在 Terrain Settings 的 Heightmap 區域中單擊 Import Raw 命令),
(4)在資源下面創建一個 Scenes 檔案夾,并把當前場景另存為 Main,

1.2添加環境
匯入需要的程式包(單擊Assets > Import Package)
(1)向地形中添加一些樹木;
(2)向場景中添加一些基本的水;
(3)向場景中水邊添加草叢,

2.角色控制器
(1)單擊 Assets > Import Package > Character Controller 命令,匯入標準的角色控制器,
(2)從 Assets\Character Controllers 檔案夾中把 First Person 控制器資源拖入場景中,
(3)把 First Person 控制器(它將被命名為 Player 并設定為藍色)放置在(160, 32, 64)處,
在 y 軸上把控制器旋轉 260°,使之面朝正確的方向,


3.添加游戲控制物件
復活點:
(1)向場景中添加一個空的游戲物件(單擊 GameObject > Create Empty 命令),并把它定位
于(160, 32, 64)處,
(2)在 Hierarchy 視圖中把空物件重命名為 SpawnPoint,

檢測器:
(1)向場景中添加一個平面(單擊 GameObject > Create Other > Plane 命令),把它定位于(86,
27, 51)處,并把該平面縮放為(10, 1, 10),
(2)在 Hierarchy 視圖中把平面重命名為 WaterHazardDetector,
(3)在 Inspector 視圖中選中 Mesh Collider 組件上的 Is Trigger 復選框

完成區域:
(1)向場景中添中一個空的游戲物件,并把它定位于(26, 32, 24)處,
(2)在 Hierarchy 視圖中把該物件重命名為 Finish,
(3)給完成區域物件添加一個燈光組件(選取該物件,單擊 Component > Rendering > Light 命令),如果它的型別還不是 Point,就把型別更改為 Point,并把范圍和強度分別設定為 35 和 3,
(4)選取完成區域物件并單擊 Component > Physics > Capsule Collider 命令,給該物件添加 一個膠囊碰撞器,在 Inspector 視圖中把 Radius 屬性改為 9,并選中 IsTrigger 復選框

游戲控制物件:
(1)向場景中添加一個空的游戲物件,
(2)在 Hierarchy 視圖中把該游戲物件重命名為 GameControl,

4.添加腳本和把腳本連接在一起
(1)對 Finish 游戲物件應用 FinishScript,

FinishScript代碼如下:
FinishScript.cs:
using UnityEngine;
using System.Collections;
public class FinishScript : MonoBehaviour {
//This is a place holder for the script that controls the game
public GameControlScript gameControlScript;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
//This states that when an object enters the finish zone, let the
//game control know that the current game has ended
void OnTriggerEnter(Collider other)
{
gameControlScript.FinishedGame();
}
}
(2)對 GameControl 物件應用 GameControlScript,

GameControlScript代碼如下:
GameControlScript.cs:
using UnityEngine;
using System.Collections;
public class GameControlScript : MonoBehaviour {
//The amount of ellapsed time
private float time = 0;
//Flags that control the state of the game
private bool isRunning = false;
private bool isFinished = false;
//place holders for the player and the spawn point
public Transform spawnPoint;
public GameObject player;
//place holders for the scripts on the character controller
public CharacterMotor motorScript;
public MouseLook lookScript;
//This resets to game back to the way it started
private void InitLevel()
{
time = 0;
isRunning = true;
isFinished = false;
//move the player to the spawn point
player.transform.position = spawnPoint.position;
//Allow the character controller to move and
//look around
motorScript.enabled = true;
lookScript.enabled = true;
}
// Use this for initialization
void Start () {
//prevent the character controller
//from looking around
motorScript.enabled = false;
lookScript.enabled = false;
}
// Update is called once per frame
void Update () {
//add time to the clock if the game
//is running
if(isRunning)
time += Time.deltaTime;
}
//This runs when the player enters the finish
//zone
public void FinishedGame()
{
isRunning = false;
isFinished = true;
//freeze the character controller
motorScript.enabled = false;
lookScript.enabled = false;
}
//This section creates the Graphical User Interface (GUI)
void OnGUI()
{
if(!isRunning)
{
string message;
if(isFinished)
message = "Click to Play Again";
else
message = "Click to Play";
if(GUI.Button(new Rect(Screen.width / 2 - 70, Screen.height/2, 140, 30), message))
{
//start the game if the user clicks to play
InitLevel ();
}
}
//If the player finished the game, show the final time
if(isFinished)
{
GUI.Box(new Rect(Screen.width / 2 - 65, 185, 130, 40), "Your Time Was");
GUI.Label(new Rect(Screen.width / 2 - 10, 200, 20, 30), ((int)time).ToString());
}
//If the game is running, show the current time
else if(isRunning)
{
GUI.Box(new Rect(Screen.width / 2 - 65, Screen.height - 115, 130, 40), "Your Time Is");
GUI.Label(new Rect(Screen.width / 2 - 10, Screen.height - 100, 20, 30), ((int)time).ToString());
}
}
}
(3)對 WaterHazardDetector 物件應用 RespawnScript,

RespawnScript代碼如下:
RespawnScript.cs:
using UnityEngine;
using System.Collections;
public class RespawnScript : MonoBehaviour {
//Place holder for the spawn point
public Transform respawnPoint;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
//This fires off when the player enters
//the water hazard
void OnTriggerEnter(Collider other)
{
//Moves the player to the spawn point
other.gameObject.transform.position = respawnPoint.position;
}
}

5.測驗游戲



總結
術語
??本實驗中使用了下面一些新術語,
????復活:復活是一個程序,玩家或物體通過它進入游戲,
????復活點:復活點是玩家或物體復活的位置,游戲中可以有一個或多個復活點,它們可以是靜止的,或者是四處移動的,
????條件:條件是一種觸發器形式,獲勝條件是使玩家贏得游戲的事件(比如積累足夠的點 數);失敗條件是使玩家輸掉游戲的事件(比如丟失所有的單擊點數),
????游戲控制器:游戲控制器規定了游戲的規則和流程,它負責知道何時贏得或輸掉游戲(或 者只是游戲結束了),可以把任何物件指定為游戲控制器,只要它總是在場景中即可,通常,把一個空物件或者 Main Camera 指定為游戲控制器,
????腳本:GameObject的行為都是被附加到其上面的組件控制,腳本本質上也是一個組件,
??在unity中創建一個腳本 ,腳本通過實作一個派生自”MonoBehaviour”的類來與unity的內部作業機制建立聯系,可以將新創建的組件型別的類作為一個藍圖,該類作為一個新型別的組件被附加到游戲物件,每次將一個腳本組件附加到游戲物件,都會創建一個該藍圖定義的物件的實體,創建的腳本檔案的檔案名必須與里面的類名相同,這樣才能將其附加到游戲物件上,
??Update函式處理游戲物件的幀更新相關的操作,可能包括移動,觸發動作以及對用戶輸入的反饋,基本上在游戲程序期間需要處理的任何事情都可以在這里面處理,Start函式在游戲開始前被unity呼叫(例如,在Update被第一次呼叫之前),因而是一個進行初始化操作的理想位置,為什么不把初始化操作放在類的建構式中,這是因為物件的構造是由編輯器處理的,在游戲開始的時候并不如想象中那樣會發生,如果為一個腳本組件定義構造器,它會和unity的正常操作發生干擾從而導致一些問題,腳本被創建之后是處于不激活狀態的,只有將它的一個實體附加到一個游戲物件之后代碼才會被激活,同時,一個游戲物件的一個型別的組件只能有一個,也就是可以附加到游戲物件的腳本組件不能超過一個,
??在本實驗中,我在 Unity 中制作了自己的第一款游戲,首先設計了游戲理念、規則和需求的多個方面,接著構建了游戲世界,并且添加了環境效果,然后添加了互動性所需的游戲物件,對那些游戲物件應用了腳本,并把它們聯系在一起,最后對游戲進行了測驗,并且注明了喜歡和不喜歡的事情,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/400383.html
標籤:其他
上一篇:Java撰寫“諸神黃昏“小游戲
