Related Posts Plugin for WordPress, Blogger...

4-14 Special Ability Animations

今天要來製作發動技能時的動作,非常簡單,原理與之前武器系統使用的Animator Override Controller相同,我們會在Weapon內放置新的Animation Clip,當發動技能時替換掉預設的攻擊用的Animation Clip。

我們在AbilityBehaviour中撰寫PlayAbilityAnimation方法,然後於繼承的子類別中繼承該方法便可以成功套用動畫。

以下提供修改後的AbilityConfig、AbilityBehaviour,以及需要套用動畫的技能SelfHealBehaviour。

AbilityConfig.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Core;

namespace RPG.Character{
 public abstract class AbilityConfig : ScriptableObject {
  [Header("Special Ability General")]
  [SerializeField] float energyCost = 10f;
  [SerializeField] GameObject particlePrefab = null;
  [SerializeField] AnimationClip abilityAnimatin = null;
  [SerializeField] AudioClip audioClip = null;

  public AbilityBehaviour AttachAbilityTo (GameObject gameObjectAttachTo){
   AbilityBehaviour behaviourComponent = GetBehaviourComponent(gameObjectAttachTo);
   behaviourComponent.SetConfig (this);
   return behaviourComponent;
  }

  public abstract AbilityBehaviour GetBehaviourComponent(GameObject gameObjectAttachTo);

  public float GetEnergyCost(){
   return energyCost;
  }

  public GameObject GetParticlePrefab(){
   return particlePrefab;
  }

  public AnimationClip GetAbilityAnimation(){
   return abilityAnimatin;
  }

  public AudioClip GetAudioClip(){
   return audioClip;
  }
 }
}

AbilityBehaviour.cs

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

namespace RPG.Character{
 public abstract class AbilityBehaviour : MonoBehaviour {

  protected AbilityConfig config;

  const string ATTACK_TRIGGER = "Attack";
  const string DEFAULT_ATTACK = "DEFAULT ATTACK";
  const float PARTICLE_CLEAN_UP_DELAY = 20f;

  public abstract void Use(GameObject target = null);

  public void SetConfig(AbilityConfig config){
   this.config = config;
  }

  protected void PlayEffectAudio(){
   // 取得音效Component
   AudioSource myAudioSource = GetComponent();
   // 從config取得技能音效
   AudioClip audioClip = config.GetAudioClip ();
   // 使用PlayOnShot播放技能音效,可避免和其他技能音效重疊在一起
   myAudioSource.PlayOneShot (audioClip);
  }

  protected void PlayAbilityAnimation(){
   AnimatorOverrideController animatorOverrideController = 
    GetComponent().GetOverrideController ();
   Animator animator = GetComponent ();
   animator.runtimeAnimatorController = animatorOverrideController;
   animatorOverrideController [DEFAULT_ATTACK] = config.GetAbilityAnimation ();
   animator.SetTrigger (ATTACK_TRIGGER);
  }

  protected void PlayParticleEffect(){
   // 初始化粒子系統,綁定到『自己』身上
   GameObject particlePrefab = Instantiate(config.GetParticlePrefab(),
    transform.position,
    Quaternion.identity,
    transform);
   // 啟動粒子系統
   particlePrefab.GetComponent().Play();
   // 播放完後自我銷毀
   StartCoroutine(DestroyParticleWhenFinished(particlePrefab));
  }

  protected void PlayParticleEffectOnTarget(GameObject target){
   // 初始化粒子系統,綁定到『目標』身上
   GameObject particlePrefab = Instantiate(config.GetParticlePrefab(),
    target.transform.position,
    Quaternion.identity,
    target.transform);
   // 啟動粒子系統
   particlePrefab.GetComponent().Play();
   // 播放完後自我銷毀
   StartCoroutine(DestroyParticleWhenFinished(particlePrefab));
  }

  IEnumerator DestroyParticleWhenFinished(GameObject particlePrefab){
   while (particlePrefab.GetComponent ().isPlaying) {
    yield return new WaitForSeconds (PARTICLE_CLEAN_UP_DELAY);
   }
   Destroy (particlePrefab);
   yield return new WaitForEndOfFrame ();
  }
 }
}

SelfHealBehaviour.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using RPG.Core;

namespace RPG.Character{
 public class SelfHealBehaviour : AbilityBehaviour {

  PlayerMovement player;

  void Start(){
   player = GetComponent ();
  }

  public override void Use(GameObject target){
   player.GetComponent ().Heal ((config as SelfHealConfig).GetExtraHealth ());
   PlayParticleEffect ();
   PlayEffectAudio ();
   PlayAbilityAnimation ();
  }
 }
}




修改好程式碼後,我在Self Heal L1中指定Ability Animation為2 Hand-Sword-Fall。


實際執行遊戲看看吧!發動補血技能時,角色有成功擺出滑稽的動作!

留言