敵を動かす①(敵キャラの作成)








EnemyHealthの改良
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()
{
// ★改良(HPスライダー)
// 動く敵には『HPスライダー』を設置しません。
// そのためHPスライダーを設置している場合だけスライダーが動作するように改良します。
if(hpSlider)
{
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);
// ★改良(HPスライダー)
if (hpSlider)
{
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);
}
}
}
}
}





【2019版】Danmaku Ⅱ(基礎2/全38回)
他のコースを見る







EnemyHealthの改良
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()
{
// ★改良(HPスライダー)
// 動く敵には『HPスライダー』を設置しません。
// そのためHPスライダーを設置している場合だけスライダーが動作するように改良します。
if(hpSlider)
{
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);
// ★改良(HPスライダー)
if (hpSlider)
{
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);
}
}
}
}
}





敵を動かす①(敵キャラの作成)