アイテムの作成⑥(アイテムのランダム出現)
![1502506a 6684 4f2d 82c9 05ca655857f2](https://codegenius.org/uploads/slide/image/4794/1502506a-6684-4f2d-82c9-05ca655857f2.jpeg)
![D0481d21 830d 4397 8b92 879db06a74d2](https://codegenius.org/uploads/slide/image/4795/d0481d21-830d-4397-8b92-879db06a74d2.jpeg)
![596ff9f1 f617 48fa 94f2 614a50afb87d](https://codegenius.org/uploads/slide/image/4796/596ff9f1-f617-48fa-94f2-614a50afb87d.jpeg)
![F1c11b06 f7fa 44f4 b7c2 343712c5888b](https://codegenius.org/uploads/slide/image/4797/f1c11b06-f7fa-44f4-b7c2-343712c5888b.jpeg)
アイテムランダム出現
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
public int enemyHP;
public Slider hpSlider;
public int scoreValue;
private ScoreManager sm;
// ★追加(アイテムランダム出現)
// 配列(ポイント)
public GameObject[] items;
private void Start()
{
hpSlider.maxValue = enemyHP;
hpSlider.value = enemyHP;
sm = GameObject.Find("ScoreLabel").GetComponent<ScoreManager>();
}
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Missile"))
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
enemyHP -= 1;
Destroy(other.gameObject);
hpSlider.value = enemyHP;
if(enemyHP == 0)
{
Destroy(transform.root.gameObject);
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
sm.AddScore(scoreValue);
// ★追加(アイテムランダム出現)
// アイテムの設定をしていない場合には何も出現しない。
if(items.Length != 0)
{
// ランダムメソッドの活用(ポイント)
int itemNumber = Random.Range(0, items.Length);
Instantiate(items[itemNumber], transform.position, Quaternion.identity);
}
}
}
}
}
![C1f5e11b 2ab0 47e3 bf82 97f1f18ea81a](https://codegenius.org/uploads/slide/image/4798/c1f5e11b-2ab0-47e3-bf82-97f1f18ea81a.jpeg)
![F0afde20 9f75 4f7b 938c 318abf90cbc2](https://codegenius.org/uploads/slide/image/4799/f0afde20-9f75-4f7b-938c-318abf90cbc2.jpeg)
![45ea5dfa 6427 4ffa 8054 1da574357c0d](https://codegenius.org/uploads/slide/image/4800/45ea5dfa-6427-4ffa-8054-1da574357c0d.jpeg)
![984df69b bee2 47c1 9932 950e847993ac](https://codegenius.org/uploads/slide/image/4801/984df69b-bee2-47c1-9932-950e847993ac.jpeg)
【2019版】Danmaku Ⅱ(基礎2/全38回)
他のコースを見る![1502506a 6684 4f2d 82c9 05ca655857f2](https://codegenius.org/uploads/slide/image/4794/1502506a-6684-4f2d-82c9-05ca655857f2.jpeg)
![D0481d21 830d 4397 8b92 879db06a74d2](https://codegenius.org/uploads/slide/image/4795/d0481d21-830d-4397-8b92-879db06a74d2.jpeg)
![596ff9f1 f617 48fa 94f2 614a50afb87d](https://codegenius.org/uploads/slide/image/4796/596ff9f1-f617-48fa-94f2-614a50afb87d.jpeg)
![F1c11b06 f7fa 44f4 b7c2 343712c5888b](https://codegenius.org/uploads/slide/image/4797/f1c11b06-f7fa-44f4-b7c2-343712c5888b.jpeg)
アイテムランダム出現
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip sound;
public int enemyHP;
public Slider hpSlider;
public int scoreValue;
private ScoreManager sm;
// ★追加(アイテムランダム出現)
// 配列(ポイント)
public GameObject[] items;
private void Start()
{
hpSlider.maxValue = enemyHP;
hpSlider.value = enemyHP;
sm = GameObject.Find("ScoreLabel").GetComponent<ScoreManager>();
}
private void OnTriggerEnter(Collider other)
{
if(other.gameObject.CompareTag("Missile"))
{
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 0.5f);
enemyHP -= 1;
Destroy(other.gameObject);
hpSlider.value = enemyHP;
if(enemyHP == 0)
{
Destroy(transform.root.gameObject);
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
sm.AddScore(scoreValue);
// ★追加(アイテムランダム出現)
// アイテムの設定をしていない場合には何も出現しない。
if(items.Length != 0)
{
// ランダムメソッドの活用(ポイント)
int itemNumber = Random.Range(0, items.Length);
Instantiate(items[itemNumber], transform.position, Quaternion.identity);
}
}
}
}
}
![C1f5e11b 2ab0 47e3 bf82 97f1f18ea81a](https://codegenius.org/uploads/slide/image/4798/c1f5e11b-2ab0-47e3-bf82-97f1f18ea81a.jpeg)
![F0afde20 9f75 4f7b 938c 318abf90cbc2](https://codegenius.org/uploads/slide/image/4799/f0afde20-9f75-4f7b-938c-318abf90cbc2.jpeg)
![45ea5dfa 6427 4ffa 8054 1da574357c0d](https://codegenius.org/uploads/slide/image/4800/45ea5dfa-6427-4ffa-8054-1da574357c0d.jpeg)
![984df69b bee2 47c1 9932 950e847993ac](https://codegenius.org/uploads/slide/image/4801/984df69b-bee2-47c1-9932-950e847993ac.jpeg)
アイテムの作成⑥(アイテムのランダム出現)