ボスのHPを回復させるオブジェクトの作成

*一定時間ごとにボスのHPを回復させる能力を持つオブジェクトの作成

(ボスサイドのスクリプト)

*「HP回復アイテム」と同じような関数を作成する。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Boss : MonoBehaviour {

    public int bossHP;
    public Text bossHPLabel;
    public Slider bossHPSlider;

    private GameObject[] effectPrefab = new GameObject[2];
    private GameObject[] itemPrefab = new GameObject[2];

    private void Awake()
    {
        effectPrefab[0] = (GameObject)Resources.Load("Hit_01");
        effectPrefab[1] = (GameObject)Resources.Load("Hit_02");
        itemPrefab[0] = (GameObject)Resources.Load("CureItem");
        itemPrefab[1] = (GameObject)Resources.Load("ShellItem");
    }

    private void Start()
    {
        bossHPLabel.text = "HP:" + bossHP;
        bossHPSlider.value = bossHP;
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Shell"))
        {
            bossHP -= 1;
            bossHPLabel.text = "HP:" + bossHP;
            bossHPSlider.value = bossHP;

            if (bossHP > 0)
            {
                GameObject effect1 = Instantiate(effectPrefab[0], transform.position, Quaternion.identity) as GameObject;
                Destroy(effect1, 1.0f);
                Destroy(other.gameObject);
            }
            else
            {
                Vector3 pos2 = transform.position;
                GameObject effect2 = Instantiate(effectPrefab[1], new Vector3(pos2.x, pos2.y + 0.5f, pos2.z), Quaternion.identity) as GameObject;
                Destroy(effect2, 1.0f);
                Destroy(other.gameObject);
                Destroy(gameObject);
                GameObject dropItem = itemPrefab[Random.Range(0, itemPrefab.Length)];

                Vector3 pos = transform.position;
                pos.y = 0.7f;
                transform.position = pos;

                Instantiate(dropItem, transform.position, Quaternion.identity);
            }
        }
    }

    // ボスのHPを増加(回復)させる関数
    public void AddBossHP(int amount){
        bossHP += amount;

        if(bossHP > 100){
            bossHP = 100;
        }

        bossHPLabel.text = "HP:" + bossHP;
        bossHPSlider.value = bossHP;
    }
}

(ガードサイドのスクリプト)

*一定時間ごとにボスのHPを回復させる関数を呼び出して実行する。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BossGuardBot : MonoBehaviour {

    public GameObject boss;
    private int timeCount = 0;

	void Update () {
        timeCount += 1;

        // ここのロジックをよく吟味すること。
        if(timeCount % 60 == 0){
            boss.GetComponent<Boss>().AddBossHP(1);
        }
	}
}