アイテムの作成⑤(敵を倒すとアイテム出現)
敵を倒すとアイテム出現
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip destroySound;
public int enemyHP;
private Slider slider;
public int scoreValue;
private ScoreManager sm;
// ★追加(アイテム出現)
public GameObject itemPrefab;
void Start()
{
slider = GameObject.Find("EnemyHPSlider").GetComponent<Slider>();
slider.maxValue = enemyHP;
slider.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;
slider.value = enemyHP;
Destroy(other.gameObject);
if (enemyHP == 0)
{
Destroy(transform.root.gameObject);
AudioSource.PlayClipAtPoint(destroySound, transform.position);
sm.AddScore(scoreValue);
// ★追加(アイテム出現)
// 敵を破壊した瞬間=敵のHPが0になった瞬間にアイテムプレハブを実体化させる。
Instantiate(itemPrefab, transform.position, Quaternion.identity);
}
}
}
}
Danmaku Ⅱ(基礎2/全24回)
他のコースを見る敵を倒すとアイテム出現
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class EnemyHealth : MonoBehaviour
{
public GameObject effectPrefab;
public AudioClip destroySound;
public int enemyHP;
private Slider slider;
public int scoreValue;
private ScoreManager sm;
// ★追加(アイテム出現)
public GameObject itemPrefab;
void Start()
{
slider = GameObject.Find("EnemyHPSlider").GetComponent<Slider>();
slider.maxValue = enemyHP;
slider.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;
slider.value = enemyHP;
Destroy(other.gameObject);
if (enemyHP == 0)
{
Destroy(transform.root.gameObject);
AudioSource.PlayClipAtPoint(destroySound, transform.position);
sm.AddScore(scoreValue);
// ★追加(アイテム出現)
// 敵を破壊した瞬間=敵のHPが0になった瞬間にアイテムプレハブを実体化させる。
Instantiate(itemPrefab, transform.position, Quaternion.identity);
}
}
}
}
アイテムの作成⑤(敵を倒すとアイテム出現)