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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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<audiosource>();
   // 從config取得技能音效
   AudioClip audioClip = config.GetAudioClip ();
   // 使用PlayOnShot播放技能音效,可避免和其他技能音效重疊在一起
   myAudioSource.PlayOneShot (audioClip);
  }
 
  protected void PlayAbilityAnimation(){
   AnimatorOverrideController animatorOverrideController =
    GetComponent<character>().GetOverrideController ();
   Animator animator = GetComponent<animator> ();
   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<particlesystem>().Play();
   // 播放完後自我銷毀
   StartCoroutine(DestroyParticleWhenFinished(particlePrefab));
  }
 
  protected void PlayParticleEffectOnTarget(GameObject target){
   // 初始化粒子系統,綁定到『目標』身上
   GameObject particlePrefab = Instantiate(config.GetParticlePrefab(),
    target.transform.position,
    Quaternion.identity,
    target.transform);
   // 啟動粒子系統
   particlePrefab.GetComponent<particlesystem>().Play();
   // 播放完後自我銷毀
   StartCoroutine(DestroyParticleWhenFinished(particlePrefab));
  }
 
  IEnumerator DestroyParticleWhenFinished(GameObject particlePrefab){
   while (particlePrefab.GetComponent<particlesystem> ().isPlaying) {
    yield return new WaitForSeconds (PARTICLE_CLEAN_UP_DELAY);
   }
   Destroy (particlePrefab);
   yield return new WaitForEndOfFrame ();
  }
 }
}
</particlesystem></particlesystem></particlesystem></animator></character></audiosource>

SelfHealBehaviour.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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<playermovement> ();
  }
 
  public override void Use(GameObject target){
   player.GetComponent<healthsystem> ().Heal ((config as SelfHealConfig).GetExtraHealth ());
   PlayParticleEffect ();
   PlayEffectAudio ();
   PlayAbilityAnimation ();
  }
 }
}
 
</healthsystem></playermovement>



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


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

留言