一、新增角色物理屬性

1、為了之後管理方便,我們將角色名稱從character_0.png更改為Player.png。

2、增加角色物理屬性Circle Collide 2D: 點擊Player的Inspector中的最下面「Add Component」,擇選Physics 2D後再從中找到Circle Collide 2D。

Untitled

Untitled

Untitled

也可直接於上方搜尋「Circle Collide 2D」

3、增加後,可從角色周圍出現在綠色圓圈看到,並透過offset及Radius,調整適當大小。

Untitled

4、增加角色物理屬性Rigidbody 2D: 點擊Player的Inspector中的最下面「Add Component」,找到Rigidbody 2D,點擊加入,並將Gravity調整為0。

Untitled

Untitled

Untitled

二、角色移動程式腳本

1、撰寫角色的程式腳本:於Assets資料夾新建名為「Scripts」的資料夾,開啟「Scripts」資料夾→於空白處點右鍵→點選Creat→點選C#Script,創建新的程式腳本,並將名稱更改為PlayerController。

Untitled

Untitled

Untitled

2、將PlayerController程式腳本加入Player的角色屬性中: 點擊Hierarchy中的Player→點擊Inspector下方的Add Compent→於搜尋欄輸入「pl」,找到並點擊剛建立的PlayerController腳本。此腳本將會套用在Player上。

Untitled

Untitled

Untitled

3、左鍵點擊兩下剛創建的PlayerController,將會開啟Visual Studio軟體(需先行安裝),此軟體將會是之後程式編輯的工具,使用的程式語言為C#,如下圖所示。

Untitled

4、撰寫程式碼: 「public Rigidbody2D theRB;」

Untitled

回到Unity,點選Player後按住Inspector中Rigidbody2D並拖曳至PlayController中TheRB變數右方的空格內。