(スクリプト)
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class MonsterHP : MonoBehaviour
{
private int monsterMaxHP = 15;
private int monsterHP;
public GameObject effectPrefab;
public AudioClip sound;
public Slider HPSlider;
public Text HPlabel;
// 追加
public GameObject body;
private Color initialColor;
private bool isGuard = false;
public GameObject guardEffectPrefab;
public AudioClip guardSound;
public GameObject doubleDamageEffect;
public AudioClip doubleDamageSound;
void Start()
{
monsterHP = monsterMaxHP;
HPSlider.maxValue = monsterMaxHP;
HPSlider.value = monsterHP;
HPlabel.text = "" + monsterHP;
// 追加
initialColor = body.GetComponent<MeshRenderer>().material.color;
}
// 追加
private void Update()
{
if (Input.GetKey(KeyCode.X))
{
// 体の色を赤色にする。
body.GetComponent<MeshRenderer>().material.color = Color.red;
isGuard = true;
}
else
{
// 元の色に戻す
body.GetComponent<MeshRenderer>().material.color = initialColor;
isGuard = false;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Stick"))
{
// 追加
// ノーガード中・・・>通常のダメージ
if (!isGuard)
{
monsterHP -= 5;
if (monsterHP == 0)
{
SceneManager.LoadScene("GameOver");
}
HPSlider.value = monsterHP;
HPlabel.text = "" + monsterHP;
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
}
// 追加
// ガード中・・・>ノーダメージ
else
{
GameObject guardEffect = Instantiate(guardEffectPrefab, transform.position, Quaternion.identity);
Destroy(guardEffect, 1.0f);
AudioSource.PlayClipAtPoint(guardSound, Camera.main.transform.position);
}
}
}
// 改良
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Yari"))
{
// 追加
// ノーガード中・・・>通常のダメージ
if (!isGuard)
{
monsterHP -= 3;
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
AudioSource.PlayClipAtPoint(sound, Camera.main.transform.position);
}
// 追加
// ガード中・・・>ダメージ2倍!
else
{
monsterHP -= 6;
GameObject doubleEffect = Instantiate(doubleDamageEffect, transform.position, Quaternion.identity);
AudioSource.PlayClipAtPoint(doubleDamageSound, Camera.main.transform.position);
}
HPSlider.value = monsterHP;
HPlabel.text = "" + monsterHP;
if (monsterHP < 0)
{
SceneManager.LoadScene("GameOver");
}
}
}
}