PlayerDamage.cs
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerDamage : MonoBehaviour
{
public GameObject Explosion;
public int hp = 5;
Slider _slider;
//private Renderer render;
private bool damaged = false;
public GameObject slider;
// Use this for initialization
void Start()
{
_slider=slider.GetComponent<Slider>();
}
// Update is called once per frame
void Update()
{
_slider.value = hp;//スライダーとHPの紐づけ
}
void OnTriggerEnter(Collider collision)
{
//衝突した相手のタグがBall2で体力が0より大きいなら
if (collision.gameObject.tag == "Ball2" && hp>0)
{
StartCoroutine("damage");
}
if (hp <= 0)
{
Instantiate(Explosion, new Vector3(transform.position.x, transform.position.y, transform.position.z), Quaternion.identity);
Destroy(this.gameObject);
}
}
IEnumerator damage()
{
if (damaged) yield break;
damaged = true;
hp -= 1;
print(hp);
//whileの中を8回繰り返す
int count = 8;
while (count > 0)
{
//render.material.color = new Color(1f, 0f, 0f, 0.5f);//ダメージを受けた時に半透明にしたかったら//を消す
yield return new WaitForSeconds(0.05f);//0.05秒待つ
//render.material.color = new Color(1f, 0f, 0f, 1f);//ダメージを受けた時に半透明にしたかったら//を消す
count--;
}
damaged = false;
}
}