0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

体力ゲージとダメージ処理※編集中

Posted at
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;
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?