300x250

1. 영역을 잡아줍니다.

2. 프레임을 잡아줍니다.

3. 체력을 표시해줄 Fill을 넣어줍니다.

체력 슬라이더바 셋팅은 끝났습니다.

 

4. 게임 오브젝트로 플레이어를 생성해주고 체력바를 위치시킬 포인트를 잡아줍니다.

 

빈오브젝트 이기때문에 눈에 안보여서 아이콘을 달아주면 확인하기 편해요.

 

Hero.cs

using System.Collections;
using UnityEngine;
using UnityEngine.Events;

namespace MonsterHpExam
{
    public class Hero : MonoBehaviour
    {
        private Transform hpGaugePoint;
        public float speed;
        private Coroutine moveRoutine;

        public UIHpGauge uiHpGauge;
        public UnityAction<float> onHit;
        private int maxHp = 100;
        private int currentHp;

        private void Start()
        {
            this.hpGaugePoint = transform.Find("HpGaugePoint").transform;
            this.uiHpGauge.Init(this.hpGaugePoint);
            this.currentHp = maxHp;
        }

        private void Update()
        {
            // 마우스 왼쪽버튼 클릭시
            if (Input.GetMouseButtonDown(0))
            {
                // 마우스 클릭시 좌표를 인게임 좌표로 변환하여 mousePos 변수에 저장
                Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                // z값은 사용을 안하므로 x, y 값만 저장후 Move
                Vector3 tpos = new Vector3(mousePos.x, mousePos.y, 0);
                this.Move(tpos);
            }
            else if (Input.GetMouseButtonDown(1))
            {
                Hit(10);
            }
        }

        public void Move(Vector3 tpos)
        {
            if (this.moveRoutine != null)
                this.StopCoroutine(moveRoutine);
            moveRoutine = this.StartCoroutine(this.MoveRoutine(tpos));
        }

        private IEnumerator MoveRoutine(Vector3 tpos)
        {
            while (true)
            {
                var dir = tpos - this.transform.position;
                this.transform.Translate(dir.normalized * this.speed * Time.deltaTime);

                var distance = dir.magnitude;
                if (distance <= 0.1f)
                    break;
                yield return null;
            }
            this.moveRoutine = null;
        }

        private void Hit(int damage)
        {            
            currentHp -= damage;
            this.onHit((float)currentHp / maxHp);
        }
    }
}

GameMain.cs

using UnityEngine;

namespace MonsterHpExam
{
    public class GameMain : MonoBehaviour
    {
        private Hero hero;
        private UIHpGauge uiHpGauge;
        // Start is called before the first frame update
        void Start()
        {
            this.hero = GameObject.FindObjectOfType<Hero>();
            this.uiHpGauge = GameObject.FindObjectOfType<UIHpGauge>();

            this.hero.onHit = (currentHpPer) =>
            {
                this.uiHpGauge.SetHpUI(currentHpPer);
            };
        }

    }
}

UIHpGauge.cs

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

namespace MonsterHpExam
{
    public class UIHpGauge : MonoBehaviour
    {
        private Image fillImg;
        private Transform targetTransform;
        private RectTransform rectTrans;

        public void Init(Transform targetTransform)
        {
            this.targetTransform = targetTransform;
            this.rectTrans = this.GetComponent<RectTransform>();
            this.fillImg = transform.Find("Fill").GetComponent<Image>();
            this.Move();
        }

        private void Move()
        {
            StartCoroutine(this.MoveRoutine());
        }

        private IEnumerator MoveRoutine()
        {
            while(true)
            {
                Vector3 screenPosition = Camera.main.WorldToScreenPoint(targetTransform.position);
                rectTrans.position = screenPosition;

                yield return null;
            }
        }
        public void SetHpUI(float currentHpPer)
        {
            if (currentHpPer > 1 || currentHpPer < 0)
                return;
            this.fillImg.fillAmount = currentHpPer;
        }
    }
}

마우스 왼쪽버튼을 클릭하면 플레이어가 움직이고 오른쪽 버튼을 클릭하면 HP가 10%씩 떨어지게 하고 테스트를 해봤습니다.

 

 

 

300x250