今回は特技を使用した時にアニメーションを再生されるようにしていきます。
アニメーションを実装するにあたって、こちらのアセットを使用します。
assetstore.unity.com
それからぴぽや倉庫様、moyasi戦闘エフェクト館様からも戦闘エフェクトをお借りします。
pipoya.net
tktknkyo.hatenadiary.jp
SpecialData.csに再生するアニメーションを入れる公開変数を宣言します
クリックで展開
[Header("再生アニメーション")] public GameObject spAnimation;
itemDataにも作っておきます。
クリックで展開
[Header("再生アニメーション")] public GameObject itemAnimation;
アニメーション再生クラスPlayEffectクラスを作成します。
クリックで展開
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayEffect : MonoBehaviour
{
public IEnumerator IEPlaySpecialEffect(Vector3 playPos, SpecialData specialData)
{
GameObject effectObject = Instantiate(specialData.spAnimation, playPos, Quaternion.identity);
while (!effectObject.GetComponent<ParticleSystem>().isStopped)
{
yield return new WaitForSeconds(0.1f);
}
Destroy(effectObject);
}
public IEnumerator IEPlayItemEffect(Vector3 playPos, ItemData itemData)
{
GameObject effectObject = Instantiate(itemData.itemAnimation, playPos, Quaternion.identity);
while (!effectObject.GetComponent<ParticleSystem>().isStopped)
{
yield return new WaitForSeconds(0.1f);
}
Destroy(effectObject);
}
}
SpecialEventクラスのIEUseAttackSpecialコルーチンにアニメーション再生の処理を追加します。
クリックで展開
DialogTextManager.instance.SetScenarios(new string[] {
player.pname + specialData.useMessage_Battle });
yield return new WaitForSeconds(1.5f);
if (specialData.UseSEID != -1)
{
SoundManager.instance.PlaySE(specialData.UseSEID);
}
if (specialData.spAnimation != null)
{
PlayEffect playEffect = new PlayEffect();
yield return StartCoroutine(playEffect.IEPlaySpecialEffect(enemy_grobal.transform.position,specialData));
}
ItemEventクラスにも処理を追加します。
クリックで展開
DialogTextManager.instance.SetScenarios(new string[] {
player.pname + "は" + itemData.itemName + "を使った!" });
yield return new WaitForSeconds(1.5f);
if(itemData.UseSEID != -1)
{
SoundManager.instance.PlaySE(itemData.UseSEID);
}
if (itemData.itemAnimation != null)
{
PlayEffect playEffect = new PlayEffect();
yield return StartCoroutine(playEffect.IEPlayItemEffect(enemy_grobal.transform.position, itemData));
}
戦闘エフェクトをテストするUIを作成します。以下の画像のように作成しました。
この画面を制御するコードをDebugUI.csに記述します。
クリックで展開
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DebugUI : MonoBehaviour
{
public Text testEffectText;
public Text spDataText;
public Button effectPlayButton;
public Button effectRButton;
public Button effectLButton;
public Transform PlayEffectPoint;
int spID = 0;
void Start()
{
gameObject.SetActive(false);
UpdateSpEffectText();
}
public void ShowDebugUI()
{
gameObject.SetActive(true);
}
public void CloseDebugUI()
{
gameObject.SetActive(false);
}
public void ProhibitEffectButtons()
{
effectPlayButton.interactable = false;
effectRButton.interactable = false;
effectLButton.interactable = false;
}
public void AllowEffectButton()
{
effectPlayButton.interactable = true;
effectRButton.interactable = true;
effectLButton.interactable = true;
}
public void PushEffectPlayButton()
{
StartCoroutine(IEPushEffectPlayButton());
}
public IEnumerator IEPushEffectPlayButton()
{
ProhibitEffectButtons();
SpecialData specialData = SpecialManager.instance.specialDatas[spID];
if (specialData.spAnimation != null)
{
PlayEffect playEffect = new PlayEffect();
yield return StartCoroutine(playEffect.IEPlaySpecialEffect(PlayEffectPoint.position, specialData));
}
else
{
SoundManager.instance.PlaySE(11);
}
AllowEffectButton();
}
public void PushEffectRButton()
{
spID++;
spID %= SpecialManager.instance.specialDatas.Length;
UpdateSpEffectText();
}
public void PushEffectLButton()
{
spID--;
if(spID <= -1) spID = SpecialManager.instance.specialDatas.Length-1;
UpdateSpEffectText();
}
public void UpdateSpEffectText()
{
int maxSpdata = SpecialManager.instance.specialDatas.Length - 1;
string spName = SpecialManager.instance.specialDatas[spID].specialName;
spDataText.text = "特技ID " + spID + "/" + maxSpdata + " " + spName;
}
}
これで戦闘エフェクトを再生できるようになりました。
youtu.be
次回は特技の全体攻撃などを実装していきたいと思います。