(1)ストーリー
- バッドアイテムを取得すると一定時間、敵の攻撃力がアップする。
- 敵の砲弾の発射間隔が短くなる。
(2)スクリプトの作成①(セッターブロックの作成)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyShotShell_2 : MonoBehaviour
{
public GameObject enemyShellPrefab;
public AudioClip sound;
public Text intervalLabel;
private int count;
private int interval = 60;
// プロパティ
// セッターブロック<セッター>の定義
public int Interval
{
set
{
interval = value;
intervalLabel.text = "発射間隔:" + interval;
}
}
private void Start()
{
intervalLabel.text = "発射間隔:" + interval;
}
void Update()
{
count += 1;
if(count % interval == 0)
{
AudioSource.PlayClipAtPoint(sound, transform.position);
GameObject enemyShell = Instantiate(enemyShellPrefab, transform.position, Quaternion.identity);
Rigidbody enemyShellRb = enemyShell.GetComponent<Rigidbody>();
enemyShellRb.AddForce(transform.forward * 500);
Destroy(enemyShell, 5.0f);
}
}
}
(3)スクリプトの作成②(プロパティの変更)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyShotUp_2 : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
public GameObject enemyShotShell;
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Player"))
{
this.gameObject.SetActive(false);
AudioSource.PlayClipAtPoint(sound, transform.position);
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
// プロパティへのアクセス
enemyShotShell.GetComponent<EnemyShotShell_2>().Interval = 10;
Invoke("Reset", 3.0f);
}
}
private void Reset()
{
enemyShotShell.GetComponent<EnemyShotShell_2>().Interval = 60;
}
}
(4)設定&再生
- UIのTextオブジェクトを使って発射間隔を表示するラベルを作成
- 設定が完了したらゲームを再生
- 最初は60フレーム間隔で発射

- アイテムを取得した瞬間、発射間隔が10に変更

- 一定時間が経過すると、発射間隔が60に戻る。
