Outline
前回(6月14日)作成した機雷をばらまく敵では、敵が後方から出現するため、初見でかわしにくいと感じた。
そのため敵の出現位置を示すアラート機能を実装した。
他にも大型の敵(6月6日作成)やボス、動きの速い敵につけると程よい緊張感を残しつつ、ゲームの難易度の調整に役立つのではと考える。
Flag
画面の上下左右の4方向それぞれで独立してアラートが鳴るようにした。
各方向のアラートにフラグを用意しアラート表示の有無を切り替えている。
PackageDownLoad(一部です)
Script(アラート)
public class WarningAlart : MonoBehaviour
{
AudioSource source;
static readonly string[] alartName =
{
"UpSideWarning ",//=0x1
"DownSideWarning",//=0x2
"LeftSideWarning ",//=0x4
"RightSideWarning"//=0x8
};
public enum AlartPosition
{
UP=0x1,
DOWN=0x2,
LEFT=0x4,
RIGHT=0x8
}
uint alartActiveState;//各方向のアラートが鳴っているか
const int AlartTime = 100;//アラート表示フレーム数
int[] alartCouter=new int[alartName.Length];
GameObject[] alart=new GameObject[alartName.Length];
// Start is called before the first frame update
void Start()
{
source = GetComponent();
source.Stop();
alartActiveState = 15;//試験用に全ての方向のアラートが鳴るようにしている
for (int i = 0; i < alartName.Length; i++)
{
alartCouter[i] = AlartTime;
alart[i] = transform.Find(alartName[i]).gameObject;
alart[i].SetActive(false);
}
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
ProcessAlart();
}
void ProcessAlart()
{
for (int i = 0;i < alartName.Length; i++)
{
if((alartActiveState & (1 << i))!=0)
{
if (alartCouter[i] == AlartTime)
{
alart[i].SetActive(true);
if(source.isPlaying==false) {
source.Play();//同時に複数アラートが出ても、鳴る効果音は1つだけ
}
}
alartCouter[i]--;
Image img= alart[i].GetComponent();
float a = Mathf.Pow(Mathf.Sin(alartCouter[i]*0.1f),2);
img.color = new Color(img.color.r,img.color.g,img.color.b,a);
if (alartCouter[i] <= 0)
{
alart[i].SetActive(false);
alartActiveState &=(uint)~(1<