//tips
//particleのスクリプト起動
Particleをスクリプトで起動する方法も確認しておく。
初期時にオンになっているPlay On Awakeを外し、下記スクリプトをParticleにアタッチ。ParticleOn()メソッドをボタンの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();
}
}
//オブジェクトの段階的縮小・拡大
オブジェクトを徐々に小さくしたり、大きくしたりする機能を考える。
下記のスクリプトをcubeにアタッチし、ボタンによりOnStretch()/OnShrink()を実行するようにした。ShrinkEffect()の場合は最小値が0にならないようにloopを1から始めている。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Shrinkingfunc : MonoBehaviour
{
public void OnStretch()
{
StartCoroutine("StretchEffect");
}
public void OnShrink()
{
StartCoroutine("ShrinkEffect");
}
IEnumerator ShrinkEffect()
{
// 繰り返し回数
int loopcount = 10;
// 更新間隔
float waitsecond = 0.5f;
// スケール設定
// オフセット値
float offsetScale = 1.0f / loopcount;
// 更新値
float updateScale = 1;
// オブジェクトの有効化
transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
for (int loop = 1; loop < loopcount; loop++)
{
// スケール更新
updateScale = updateScale - offsetScale;
transform.localScale = new Vector3(updateScale, updateScale, updateScale);
yield return new WaitForSeconds(waitsecond);
}
// 最終スケール
transform.localScale = new Vector3(offsetScale, offsetScale, offsetScale);
}
IEnumerator StretchEffect()
{
// 繰り返し回数
int loopcount = 10;
// 更新間隔
float waitsecond = 0.5f;
// スケール設定
// オフセット値
float offsetScale = 1.0f / loopcount;
// 更新値
float updateScale = 0;
// オブジェクトの有効化
transform.localScale = new Vector3(0.0f, 0.0f, 0.0f);
for (int loop = 0; loop < loopcount; loop++)
{
// スケール更新
updateScale = updateScale + offsetScale;
transform.localScale = new Vector3(updateScale, updateScale, updateScale);
yield return new WaitForSeconds(waitsecond);
}
// 最終スケール
transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
}
}
コライダーも小さくしようか考えたが、流石に他のプレイヤーとの兼ね合いもあるので従来の大きさのままにしている。場合によっては、コライダーのサイズも一緒に小さくする。
次にオブジェクトのRendererを変更し、透過させるようにする。透過のスクリプトをアタッチする際にはオブジェクトのmaterialのrenderingモードを事前にOpaque以外に設定しておく。今回はFadeに設定して実行した。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TransparentEffect : MonoBehaviour
{
Color color;
void Start()
{
color = GetComponent<Renderer>().material.color;
OnTransparent();
}
public void OnTransparent()
{
StartCoroutine("TransparentEffectOn");
}
IEnumerator TransparentEffectOn()
{
// 繰り返し回数
int loopcount = 10;
float waitsecond = 0.5f;
float offsetcolor = 1.0f / loopcount;
// 更新値
float updatecolor = 1;
// オブジェクトの有効化
color.a = 1;
for (int loop = 1; loop < loopcount; loop++)
{
// スケール更新
updatecolor = updatecolor - offsetcolor;
color.a = updatecolor;
GetComponent<Renderer>().material.color = color;
yield return new WaitForSeconds(waitsecond);
}
// 最終
yield return new WaitForSeconds(1);
color.a = 1;
GetComponent<Renderer>().material.color = color;
}
}
次はMinimapで作成したレーダーの索敵範囲から外れる方法を模索する。
基本的にはプレイヤーなどのオブジェクトの子要素にminmapに表示するためのminimaplayerをつけた色付きオブジェクトを配置しているが、そのminimaplayerをmaincameraの描写layerでもminimapcameraのlayerでもないものに変えることで、レーダーに引っかからない方法を実現する。
下記スクリプトをminimaplayerをつけた色付きオブジェクトにアタッチし、ボタンで Layerchange()を発動できるようにする。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeLayer : MonoBehaviour
{
public void Layerchange()
{
this.gameObject.layer = LayerMask.NameToLayer("Water");
}
}