3-10 Create An Area Of Effect Ability
延續上一章跟大家介紹特殊技能系統,本章要製作範圍攻擊的技能,如果大家看不懂下面做的事情,請先看前一章唷。
3-9 Special Abilities System Overview
首先,請大家在Special Abilities資料夾中新增Area Effect資料夾。
在Area Effect資料夾中,新增兩個Script,AreaEffectBehaviour跟AreaEffectConfig。
由於本次要製作範圍攻擊,請大家先閱讀一下Untiy官方的介紹:
https://docs.unity3d.com/ScriptReference/Physics.SphereCastAll.html
Casts a sphere against all colliders in the scene and returns detailed information on each collider which was hit. This is useful when a Raycast does not give enough precision, because you want to find out if an object of a specific size, such as a character, will be able to move somewhere without colliding with anything on the way.
AreaEffectBehaviour.cs:
AreaEffectConfig.cs:
很簡單吧!只要有特殊技能系統,往後要再新增各種不同類型的技能都非常容易跟方便。接著,在Project視窗Create/RPG/Special Ability/Area Effect。
將新增的ScriptableObject命名為AOE L1。
將AOE L1的參數設定為自己想要的數值。
然後在Player的Abilities的Size中設定為2,將AOE L1拉到第一個技能。
3-9 Special Abilities System Overview
在Area Effect資料夾中,新增兩個Script,AreaEffectBehaviour跟AreaEffectConfig。
由於本次要製作範圍攻擊,請大家先閱讀一下Untiy官方的介紹:
https://docs.unity3d.com/ScriptReference/Physics.SphereCastAll.html
Casts a sphere against all colliders in the scene and returns detailed information on each collider which was hit. This is useful when a Raycast does not give enough precision, because you want to find out if an object of a specific size, such as a character, will be able to move somewhere without colliding with anything on the way.
簡而言之,就是可以對場景中投射出一個球體,並返回所有碰撞到這個球體的詳細訊息。比方說我想要測試看看特定大小GameObjecy如Player角色,是否能夠在某處和其他的物件碰撞,就能使用SphereCastAll測試看看,本章要介紹的範圍攻擊亦能適用。可惜找不太到中文教學,似乎撰寫這方面介紹的中文文章不多。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Core;
namespace RPG.Character{
 public class AreaEffectBehaviour : MonoBehaviour, ISpecialAbility {
  AreaEffectConfig config;
  public void SetConfig(AreaEffectConfig config){
   this.config = config;
  }
  public void Use(AbilityParams useParams){
   RaycastHit[] hits = Physics.SphereCastAll (
                        transform.position,  // 以Player為中心點
                        config.GetRadius (), // 掃描的球體半徑範圍
                        Vector3.up,    // 掃描的方向不重要,因為我們發動的是Player為原點的範圍技
                        config.GetRadius ()  // 掃描距離設定成跟掃描半徑一樣即可
                       );
   foreach (RaycastHit hit in hits) {
    Enemy enemy = hit.collider.gameObject.GetComponent ();
    if (enemy != null) {
     float damageToDeal = useParams.baseDamage + config.GetDamageToEachTarget ();
     enemy.TakeDamage (damageToDeal);
    }
   }
  }
  // Use this for initialization
  void Start () {
  }
 }
}
 
AreaEffectConfig.cs:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace RPG.Character{
 [CreateAssetMenu(menuName= ("RPG/Special Ability/Area Effect"))]
 public class AreaEffectConfig : SpecialAbilityConfig {
  [Header("Area Effect Specific")]
  [SerializeField] float radius = 5f;
  [SerializeField] float damageToEachTarget = 15f;
  public override ISpecialAbility AddComponent (GameObject gameObjectAttachTo){
   var behaviourComponent = gameObjectAttachTo.AddComponent ();
   behaviourComponent.SetConfig (this);
   return behaviourComponent;
  }
  public float GetDamageToEachTarget(){
   return damageToEachTarget;
  }
  public float GetRadius(){
   return radius;
  }
 }
}
 
很簡單吧!只要有特殊技能系統,往後要再新增各種不同類型的技能都非常容易跟方便。接著,在Project視窗Create/RPG/Special Ability/Area Effect。
將新增的ScriptableObject命名為AOE L1。
將AOE L1的參數設定為自己想要的數值。
然後在Player的Abilities的Size中設定為2,將AOE L1拉到第一個技能。
執行遊戲看看吧!只要按下滑鼠右鍵就可以發動第一個技能,確實成功發動範圍攻擊了!







留言
張貼留言