當前位置:
首頁 > 知識 > 巧用Unity 2D功能:只需六步開發簡單的2D UFO遊戲

巧用Unity 2D功能:只需六步開發簡單的2D UFO遊戲

無論我們現在可以選擇多少種類繁多的,擁有逼真畫面感的遊戲,2D遊戲始終會在玩家們的心中佔據一席之地,而對於想邁入開發者行列的同學們來說,先嘗試做一款2D遊戲無疑是合適的起點。Unity中的2D功能讓開發此類遊戲更加的便捷,這篇教程將教大家使用Unity 2D功能製作一個非常簡單的UFO遊戲,整個過程只需要6個步驟,適合Unity初學者以及對Unity的2D功能不甚熟悉的開發者們,希望Unity能讓你初嘗遊戲開發的樂趣,為以後的開發之路奠定基礎,你準備好完成這款簡單的2D遊戲了嗎?

第一步 新建項目設置場景

首先下載項目所需的資源,或者在Asset Store中下載Unity官方的2D UFO Tutorial。

巧用Unity 2D功能:只需六步開發簡單的2D UFO遊戲

新建2D項目,在Assets文件夾下新建三個文件夾:Prefabs、Scripts、Scenes。將下載的資源導入項目,會看到Sprites文件夾。將Sprites文件夾下的Background和UFO圖片拖拽至層級視圖(Hierarchy),將UFO重命名為Player。如下:

巧用Unity 2D功能:只需六步開發簡單的2D UFO遊戲

依次點擊菜單項Edit > Project Settings > Tags and Layers,新建三個Sorting Layer分別命名為Background、Pickups和Players,然後設置各Sprite的Sorting Layer。

巧用Unity 2D功能:只需六步開發簡單的2D UFO遊戲

將Player的Scale設為(0.75, 0.75, 0),如下:

巧用Unity 2D功能:只需六步開發簡單的2D UFO遊戲

將Main Camera的Size設為16.5,背景顏色設為RGB(32, 32, 32),如下:

巧用Unity 2D功能:只需六步開發簡單的2D UFO遊戲

將場景保存到新建的Scenes文件夾下,任意命名即可,本例中命名為Main。

第二步 添加控制主角的腳本

首先為Player遊戲對象添加Rigibody 2D組件。然後新建腳本CompletePlayerController

用於控制Player對象,腳本代碼如下:

using UnityEngine;

using System.Collections;

//Adding this allows us to access members of the UI namespace including Text.

using UnityEngine.UI;

public class CompletePlayerController : MonoBehaviour {

public float speed; //Floating point variable to store the player』s movement speed.

public Text countText; //Store a reference to the UI Text component which will display the number of pickups collected.

public Text winText; //Store a reference to the UI Text component which will display the 『You win』 message.

private Rigidbody2D rb2d; //Store a reference to the Rigidbody2D component required to use 2D Physics.

private int count; //Integer to store the number of pickups collected so far.

// Use this for initialization

void Start()

{

rb2d = GetComponent ();

count = 0;
//Initialze winText to a blank string since we haven"t won yet at beginning. winText.text = "";
//Call our SetCountText function which will update the text with the current value for count. SetCountText (); }

{

//Store the current horizontal input in the float moveHorizontal. float moveHorizontal = Input.GetAxis (「Horizontal」);

//Store the current vertical input in the float moveVertical. float moveVertical = Input.GetAxis (「Vertical」);

//Use the two store floats to create a new Vector2 variable movement.

Vector2 movement = new Vector2 (moveHorizontal, moveVertical);

rb2d.AddForce (movement * speed);

}

//OnTriggerEnter2D is called whenever this object overlaps with a trigger collider. void OnTriggerEnter2D(Collider2D other)

{

//Check the provided Collider2D parameter other to see if it is tagged 「PickUp」, if it is…

if (other.gameObject.CompareTag (「PickUp」))

{

//… then set the other object we just collided with to inactive. other.gameObject.SetActive(false);

//Add one to the current value of our count variable.

//Update the currently displayed count by calling the SetCountText function. SetCountText ();

}

}

//This function updates the text displaying the number of objects we』ve collected and displays our victory message if we』ve collected all of them. void SetCountText() {

countText.text = 「Count: 」 + count.ToString ();

if (count >= 12)

}

}

依次點擊Edit > Project Settings > Input設置控制鍵,如下:

巧用Unity 2D功能:只需六步開發簡單的2D UFO遊戲

將Player對象的Rigibody 2D組件的Gravity Scale屬性設為0以防止Player向下墜落,並將PlayerController腳本的Speed屬性設為10。

第三步 添加碰撞

為Player添加Circle Collider 2D組件並將Radius屬性設為2.15,如下:

巧用Unity 2D功能:只需六步開發簡單的2D UFO遊戲

接下來為背景添加碰撞體,讓玩家不要跑出牆壁。為背景邊框添加4個Box Collider 2D組件,各組件屬性設置如下:

巧用Unity 2D功能:只需六步開發簡單的2D UFO遊戲

第四步 讓攝像機跟隨主角

新建腳本CompleteCameraController綁定到Main Camera上,用於控制相機跟隨主角移動。腳本代碼如下:


using UnityEngine;

using System.Collections;

public class CompleteCameraController : MonoBehaviour {

//Public variable to store a reference to the player game object

private Vector3 offset;

void Start ()

//Calculate and store the offset value by getting the distance between the player』s position and camera』s position.

offset = transform.position - player.transform.position;

// LateUpdate is called after Update each frame

void LateUpdate ()

// Set the position of the camera』s transform to be the same as the player』s, but offset by the calculated offset distance.

transform.position = player.transform.position + offset;

}

}

將Player遊戲對象賦給CameraController腳本的player欄位。

第五步 設置寶石

將Sprites文件夾下的Pickup圖片拖拽至層級視圖,將其Sorting Layer設為Pickup並添加Circle Collider 2D組件。然後新建腳本CompleteRotator用於旋轉寶石,腳本代碼如下:


using UnityEngine;

using System.Collections;

//Update is called every frame

{

//Rotate thet transform of the game object this is attached to by 45 degrees, taking into account the time elapsed since last frame.

transform.Rotate (new Vector3 (0, 0, 45) * Time.deltaTime);

}

}

將該遊戲對象拖拽至項目視圖(Project)的Prefabs文件夾下存為預設體,在層級視圖複製出11個寶石對象。然後在層級視圖新建GameObject命名為Pickups,並將這12個對象均拖至Pickups下,分別設置合適的位置,如下圖:

巧用Unity 2D功能:只需六步開發簡單的2D UFO遊戲

將Pickup預設體的Tag設為PickUp,並將所有Pickup對象的Circle Collider 2D組件的isTrigger屬性勾選上:

巧用Unity 2D功能:只需六步開發簡單的2D UFO遊戲

第六步 添加UI

右鍵點擊層級視圖,在彈出菜單中選擇UI > Text新建文本,層級視圖中除了Text外還會添加Canvas和EventSystem組件。將新建的Text命名為CountText,在檢視面板中打開Rect Transform的錨點設置面板後按下Shift+Alt/Option鍵點選左上角,讓Text自動吸附到界面左上角並選擇合適的文本顏色,如下圖:

巧用Unity 2D功能:只需六步開發簡單的2D UFO遊戲

再次新建Text命名為WinText,將字體大小設為24,對齊位置設為中間並選擇合適的顏色,如下圖:

巧用Unity 2D功能:只需六步開發簡單的2D UFO遊戲

最後,將剛才新建的兩個Text分別賦給PlayerController腳本的CountText及WinText欄位,如下圖:

巧用Unity 2D功能:只需六步開發簡單的2D UFO遊戲

到此就大功告成了,最後運行效果如下:

巧用Unity 2D功能:只需六步開發簡單的2D UFO遊戲

同學們如果想掌握更多Unity 2D開發的知識與技巧,通過更系統的學習變身Unity技術達人,創造出夢想中的遊戲,請關注「技術閃耀未來-Unity全球技術校園行」。

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 青峰科技 的精彩文章:

Go語言:成長的十年
Cplusplus,學習多態總結,編程學習
Spring5源碼解析-Spring中的bean工廠後置處理器
C++—const volatile mutable的用法
騰訊雲DevOps流水線的應用與實踐

TAG:青峰科技 |