//tips
//UIの実装調整
キャラクターの人狼側UIに機能を追加していく。ボタンドラッグにより短距離瞬間移動できる機能を付与する。以前作成したReceiveEventスクリプトをベースに考える。
これには別途操作するボタンに対してEventtriggerコンポーネントが必要でPointerupとDragの機能を使用する。プレイヤーにはPlayerFlashスクリプトをアタッチする。
クールダウンの機能は現段階では必要ないのでReceiveEventスクリプトを修正していく。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ReceiveEvent : MonoBehaviour
{
Vector3 originpos;
Vector3 movepos;
Vector3 heading;
Vector3 direction;
public Vector3 directionfix;
public bool onflash = false;
bool cooldown = false;
float countTime = 0;
[SerializeField]
int maxtime = 3;
private void Start()
{
Transform transform = GetComponent<RectTransform>();
originpos = transform.position;
}
/*
void Update()
{
if (cooldown)
{
countTime += Time.deltaTime; //スタートしてからの秒数を格納
GetComponent<Image>().fillAmount = countTime / maxtime;
if (countTime >= 3.0f)
{
cooldown = false;
countTime = 0;
}
}
}
*/
public void MyDragUI()//Drag
{
if (cooldown != true)
{
transform.position = Input.mousePosition;
}
}
public void MyDragExit()//pointerupへ
{
movepos = transform.position;
Dragsimulate();
transform.position = originpos;
//cooldown = true;
}
/*
public void MyDragIn()//pointerdown
{
PlayerWarp playerWarp = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerWarp>();
playerWarp.GhostActive();
}
*/
public void Dragsimulate()
{
heading = movepos - originpos;
var distance = heading.magnitude;
direction = heading / distance;
Debug.Log(direction);
directionfix = new Vector3(direction.x,0,direction.y);//2dから3dへ変換
Debug.Log(directionfix);
onflash = true;
}
}
これで想定通りの動きをさせることができた。最終的には各スキルは一度しか使用できなくする。
次にボタンクリックによりオブジェクトの子要素に配置したパーティクルをplayさせる方法を考える。
一つのオブジェクトにしかparticleを配置していないケースは簡単で下記のスクリプトをつけたオブジェクトとUIのonclickを連動させれば良い。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParticlePlay : MonoBehaviour
{
private ParticleSystem particle;
void Start()
{
particle = GetComponent<ParticleSystem>();
particle.Stop();
}
public void ParticleOn()
{
particle.Play();
StartCoroutine("PartcleStop");
}
private IEnumerator PartcleStop()
{
yield return new WaitForSeconds(2);
particle.Stop();
}
}
ただ、今回はparticleがついている複数のオブジェクトで一度にparticle操作を行いたい。ParticleSystemの取得方法を配列に変更、FindGameObjectsWithTagでオブジェクトを取得し格納、foreachで格納要素に全て処理という流れで実装できないか試す。
新たにParticlePlayarrayスクリプトを作成し、空オブジェクトにアタッチ。ParticleOn()をボタンクリックから実行できるよう下記に変更。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ParticlePlayarray : MonoBehaviour
{
GameObject[] particleobjs;
void Start()
{
particleobjs = GameObject.FindGameObjectsWithTag("Particles");
foreach (GameObject obs in particleobjs)
{
ParticleSystem particle = obs.GetComponent<ParticleSystem>();
particle.Stop();
}
}
public void ParticleOn()
{
StartCoroutine("PartcleStop");
}
IEnumerator PartcleStop()
{
foreach (GameObject obs in particleobjs)
{
ParticleSystem particle = obs.GetComponent<ParticleSystem>();
particle.Play();
}
yield return new WaitForSeconds(2);
foreach (GameObject obs in particleobjs)
{
ParticleSystem particle = obs.GetComponent<ParticleSystem>();
particle.Stop();
}
}
}
無事に複数オブジェクトを検索し、particleを操作できるようになった。その際対象オブジェクトにtagをつけるのを忘れない。
次の一撃のみダメージ量を増加させるスキルを実装する。wolfmodeの時のみ発動できるという条件をつけ、これもboolのフラグで実装する。
OnTriggerEnter(Collider other)が接触の瞬間を表すので、ultimateflagで接触時の一撃および一ターゲットのみを抽出できると考えた。
if (wolfmode)
{
decoy.hp -= powerEnemy*2;
Debug.Log("wolfmode");
if (ultimateflag)
{
decoy.hp -= powerEnemy * 100;
ultimateflag = false;
}
}
public void ultimateattack()
{
ultimateflag = true;
}
ダメージが2回入ってしまうのはAttackmotion()メソッドで実行されるWaitAnimationfin()コルーチンのyield return new WaitForSecondsの時間が長すぎることで生じていることがわかったので、1秒ではなく0.2秒に変更した。先にも検討した通りモーションそのものの発生時間だと短すぎて判定が入らないので秒数で管理している。
秒数にすることで攻撃範囲も変わるので、そこは表示コライダーの大きさを調整することでカバーする。
RabbitのキャラUI機能の実装は完成したので、次にbearの操作/UIの実装に移る。bearにはパンチモーションをつけたがなぜかモーションの途中で待機モードに入ってしまう問題が起きたが、animatorで次の待機モーションへのtransition durationを0にすることで最後までモーションを行わせることができた。