步驟一 建立滾球
- Hierarchy界面下建立一個球體(sphere),命名為Player;建立地面,命名為Plane。
- 給Player增加一個元件Rigidbody。
- Project界面下建立檔案夾Scripts,在裡面建立C#腳本PlayController。基本思想是按下方向鍵就施加向相應方向的力。由于是實體模型,可以用FixedUpdate()代替Update()保持不同幀率下的穩定性。代碼如下:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
}
- 用VS編譯,将此腳本拖到Player上。可以在Inspector界面下設定speed,即移動速度。進入game界面時按箭頭移動球體。
步驟二 綁定相機
事實上,我們在Hierarchy界面下把相機拖到Player上就完成了綁定,通常這樣是用作第一人稱視角。這樣的問題是當Player旋轉的時候,相機也會跟着旋轉,這樣在進入game界面時,會有天旋地轉的感覺。是以,尤其是第三人稱視角,我們隻需要相機和Player的相對位置保持一緻。我們使用腳本CameraConller控制。代碼如下:
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour {
public GameObject player;
private Vector3 offset;
void Start ()
{
offset = transform.position - player.transform.position;
}
void LateUpdate ()
{
transform.position = player.transform.position + offset;
}
}
這裡解釋一下transform。他是所有Object都有的元件,其中transform.position、transform.ratation、transform.scale分别存儲了這個Object的位置、角度、大小。
将此腳本拖到MainCamera上,點選Inspector界面中Player右邊的齒輪,選擇Player。
這裡采用LateUpdate(),這樣他就時所有Update中最後更新的。
步驟三 建立牆壁
首先建立一個空物體,命名為Walls。再在Plane周圍建立4面牆壁,隸屬于Walls,分别命名為West Wall,East Wall,North Wall,South Wall。
步驟四 建立目标體
首先在腳本PlayerController中增加語句:
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Pick up"))
{
other.gameObject.SetActive(false);
}
}
這個函數的意思是如果Player如果碰到了某種觸發器,就執行這個函數。而這個函數裡面的意思是,如果這個other的觸發标簽(Tag)是"Pick Up",那麼設定這個other物體不活躍,相當于該物體在Inspector界面下取消了第一個勾,進而設定為消失。
接下來就是設定Tag。首先我們在Project檔案夾下建立Prefabs檔案夾,建立Cube命名為Pick Up,可以塗為黃色。輕按兩下進入,從上向下找到Tag,選擇Add Tag,點加号,并命名為Pick Up。回到首頁面,建立空物體Pick Ups,在Pick Ups下使用若幹Prefabs中的Pick Up,拷貝若幹。結果如圖:
最後進入Prefabs中的Pick Up中,在Box Collider中點選Is Trigger。建立Rigidbody,勾選Use Gravity和Is Kinematic。
步驟五 顯示分數和結束遊戲
進入PlayerController腳本,建立私有變量count用來記錄分數。在start()函數中初始化為0。每吃到一個“Pick Up”,count+1。
...
private int count
void Start()
{
rb = GetComponent<Rigidbody>();
count = 0;
}
...
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count++;
}
}
接下來的問題就是怎麼顯示這個分數了
在Hierarchy點create->UI->Text,重命名為Count Text。
在Inspector中Rect Transform菜單下點選大方塊,按住Shift+Alt鍵選左上角。PosX改為10,PosY改為-10。
打開腳本PlayerController,增加代碼。
...
using UnityEngine.UI;
public class PlayerController : MonoBehaviour
{
public Text CountText;
void Start()
{
...
SetCountText();
}
...
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Pick Up"))
{
...
SetCountText();
}
}
void SetCountText()
{
CountText.text = "Count: " + count.ToString();
}
}
建立新的文本對象,重命名為Win Text,Paragraph中設為居中。PosY設為75。
增加代碼如下:
public Text WinText;
void Start()
{
...
WinText.text = "";
}
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Pick Up"))
{
...
if(count >= 12)
{
WinText.text = "You Win!";
}
}
}
點選Player,分别連結對應的文本。
步驟六 導出遊戲
首先儲存場景、儲存項目。
File–>Build Setting
選擇合适的平台,把場景檔案拖到上方的框中(Scenes In Build)。點選Build,在項目中建立一個檔案夾Build,選擇此檔案夾。點選Build and Run。
好了,大家愉快的玩遊戲吧!
參考文獻
https://www.w3cschool.cn/unity3d_jc/unity3d_jc-fntk2d6t.html
https://docs.unity3d.com/2018.3/Documentation/ScriptReference/
代碼
https://github.com/lyksunny/Unity/tree/master/RollBall