RPG Character Mecanim Animation Pack 教學

CG 數位學習網 > Unity RPG Character Mecanim Animation Pack 教學



RPG Character Mecanim Animation Pack 近身攻擊設定教學

RPG Character Mecanim Animation Pack 的原始設計是採用遊戲手把操作,使用手把的方向鍵按「上∕下」切換雙手武器,按「左∕右」可以取出或收回左右手的武器。可是鍵盤上沒有對應的方向鍵,所以這次先修改程式,讓玩家使用特定的按鈕取出與收回武器。

 

開啟 [ RPG Character Animation Pack > Code ] 資料夾的 GUIControls 腳本,搜尋 Right Sword 找到右手劍的程式碼。

這段程式碼的功能是先判斷玩家右手的武器不是拿劍,按下 Right Sword 按鈕時,右手將會取出劍。

if(rpgCharacterController.rightWeapon != 9){
           if(GUI.Button(new Rect(1165, 530, 100, 30), "Right Sword")){
           StartCoroutine(rpgCharacterController._SwitchWeapon(9));
           }
}

動作遊戲需要反應迅速,希望讓玩家使用鍵盤取出武器的話,可以增加一些程式碼:

if(rpgCharacterController.rightWeapon != 9){
                                    
           if(GUI.Button(new Rect(1165, 530, 100, 30), "Right Sword")){
                     StartCoroutine(rpgCharacterController._SwitchWeapon(9));
           }

           if (Input.GetKeyDown(KeyCode.Keypad1)){
                     StartCoroutine(rpgCharacterController._SwitchWeapon(9));
           }
                                        
}

 

玩家使用鍵盤右側的數字 1 取出劍,按下 2 使用流星槌,按下 3 使用藍波刀。如果能夠理解的話,您就能讓玩家切換各種武器了。

若需要隱藏畫面的搖桿圖示,選擇場景中的 [ Camera > GUI Camera > Controls ] 物件後將其關閉。至於畫面上的 UI 按鈕使用雙斜線註解就不會顯示。

選取 Sword 劍的模型,實際上 Sword 是 B_R_Hand 右手的子物件,所以是隨著手掌移動的。新增方塊碰撞器,按下 Play 測試遊戲,就可以砍物件了。

不過攻擊之後玩家可能原地旋轉,這是因為 Rigidbody 旋轉沒有凍結的關係。選取場景中的玩家角色,然後將 Rigidbody 下方的 Constraints > Freeze Rotation 的 Y 軸勾選即可。

如何讓拳腳的攻擊對敵人造成損害?請在手腳的骨架  ( B_L_Hand、B_R_Hand、B_L_Foot、B_R_Foot ) 加上方塊碰撞器。

 

製作玩家攻擊

預設的情況,玩家攻擊敵人時只有動作,並不會真正對敵人造成傷害,這裡以「雙手劍」與「單手劍」說明如何讓敵人能夠受到玩家的攻擊。

選取 2Hand-Sword 雙手劍,因為沒有碰撞器,無法判定是否砍到敵人。

如果直接在武器上加入方塊碰撞器,發現不容易調整位置與角度的話,也可以使用方塊當作武器的子物件。

加入方塊,重新命名為「2Hand-Sword-Collider」,拖曳到 2Hand-Sword 設為雙手劍的子物件,執行 Reset 後調整位置與大小。

新增一個 C# 腳本,並命名為「EnemyController」,功能是計算敵人的血量,被玩家攻擊時血量減少。程式碼如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyController : MonoBehaviour {

     public int enemyHealth;

     void OnTriggerEnter(Collider other){
         if(other.gameObject.tag == "PlayerAttack"){
              Destroy (gameObject);
          }
     }
}

遊戲中的各種武器,實際上對敵人的傷害也會有所差異,例如雙手劍的攻擊力通常高於單手劍,當然雙手劍的動作比較大,比較容易露出破綻而受到反擊。這就是遊戲平衡的設計。

如果腳本這樣撰寫,不但需要增加 Tag 標籤,而且程式碼比較冗長,以後的修改維護可能比較不容易。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyController : MonoBehaviour {

     public int enemyHealth;

     void Update () {
         Debug.Log (enemyHealth);
     }

     void OnTriggerEnter(Collider other){
         if(other.gameObject.tag == "2Hand-Sword"){
              enemyHealth -= 10 ;
         }
         if(other.gameObject.tag == "Sword"){
              enemyHealth -= 5 ;
         }
     }
}

新增一個 C# 腳本,命名為「WeaponController.cs」,程式碼如下:

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WeaponController : MonoBehaviour {

public int weaponAttack; // 武器的攻擊力

}


將此腳本分別套用到不同的武器上,然後設定其攻擊力。

 

將 EnemyController.cs 的程式碼修改如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyController : MonoBehaviour {

     public int enemyHealth;

     void Update () {
         Debug.Log (enemyHealth);
     }

     void OnTriggerEnter(Collider other){
         if(other.gameObject.tag == "PlayerAttack"){
              int weaponAttack = other.GetComponent<WeaponController> ().weaponAttack;
              enemyHealth -= weaponAttack ;
         }
     }
}

 

我們希望在遊戲畫面上顯示血量,這次搭配 Energy Bar Toolkit v4.0.1 製作血條。

首先執行 [ GameObject > UI > Canvas ] 建立 UI 畫布,然後再增加 Filled Bar 血條。

讓血條顯示於方塊的上方,選取血條並套用 Energy Bar Follow Object 腳本,然後將方塊拖曳到 Follow Object 欄位,將攝影機拖曳到 Camera 欄位。將 Offset Y 的數值增加,例如設定 Y=60 即可顯示於上方。

 

 

現在武器已經可以對敵人造成不同的傷害,但仍有重複扣血的問題(武器碰到 2 次的關係),這部份留著下次研究吧!

 

 



~ CG 數位學習網版權所有 ~