슈팅게임에서 썼던 방식을 그대로 HP바와 골드를 표시해줬다.


HP바는 아래와같이 이미지 3개를 합쳐서 HpBar라는 오브젝트의 자식으로 들어가게끔 해주었다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
 
public class HpBar : MonoBehaviour {
 
    public GameObject hpBarLeft = null;
    public GameObject hpBarMiddle = null;
    public GameObject hpBarRight = null;
 
    private GameObject Left = null;
    private GameObject Middle = null;
    private GameObject Right = null;
 
    // 0.5배한 기준
    private float halfLR = 0.0225f;
    private float halfmiddle = 0.045f;
 
    public float HpPercent = 1.0f;
    // Use this for initialization
    void Start () {
        Middle = Instantiate(hpBarMiddle, transform);
        Left = Instantiate(hpBarLeft, transform);
        Right = Instantiate(hpBarRight, transform);
 
        Middle.transform.localScale = new Vector3(Middle.transform.localScale.x * 6.0f, Middle.transform.localScale.y);
        Middle.transform.position = new Vector3(transform.position.x, transform.position.y + 0.5f);
        Left.transform.position = Middle.transform.position - new Vector3(halfLR + halfmiddle * 6.0f, 0.0f);
        Right.transform.position = Middle.transform.position + new Vector3(halfLR + halfmiddle * 6.0f, 0.0f);
    }
    
    // Update is called once per frame
    void Update () {
        // 프리팹의 스케일을 0.5배 해놨으므로 0.5를 곱해줌. 그리고 6배한것(실제로는 3배)
        Middle.transform.localScale = new Vector3(0.5f * HpPercent * 6.0f, Middle.transform.localScale.y);
 
        Vector3 MiddlePos = Left.transform.position + new Vector3(halfLR + halfmiddle * 6.0f * HpPercent, 0.0f);
        Middle.transform.position = MiddlePos;
 
        Vector3 RightPos = Middle.transform.position + new Vector3(halfLR + halfmiddle * 6.0f * HpPercent, 0.0f);
        Right.transform.position = RightPos;
    }
}
cs

픽셀 계산해서 좌표를 계산해 HpBar의 포지션기준으로 y값만 0.5 올려 거기에 가운데바를 생성하고

가운데바의 양끝에 왼쪽바와 오른쪽바를 추가시켜주었다.



그리고 몬스터의 스크립트에서 HP바를 생성

충돌시 깎인 체력만큼 HpPercent를 넘겨주어 그만큼 HP바의 x축 스케일이 줄어들게 하였다.

1
2
3
4
5
6
7
8
9
10
11
12
13
// HP바를 생성
mobHpBar = Instantiate(HpBar, transform.position, Quaternion.identity, transform);
mobHpBar.transform.localScale *= 0.5f;
 
...
 
// 충돌부분
if (collision.tag == "Thunder")
{
    hp--;
    mobHpBar.GetComponent<HpBar>().HpPercent = (float)hp / (float)maxhp;
    Destroy(collision.gameObject, 0.2f);
}
cs



HP바가 적용된 모습






골드 표시는 이전에 스코어를 표시해주는 스크립트를 그대로 가져다 썼다.

이전에 코코스에서 출력했던방식과 거의 비슷하여 설명은 생략. 

궁금하신분들은 스크립트 첨부하니 받아서 보시길 바란다.

ImageScore.cs



그리고 게임데이터를 관리할 게임컨트롤러(매니저)를 생성하여 돈을 관리하고

돈을 증가시키는 함수, 감소시키는 함수, 가진 돈을 알려주는 함수, 갱신해주는 함수를 만들었다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class GameController : MonoBehaviour {
 
    private int money = 300;
    // Use this for initialization
    void Start () {
        setMoney();
    }
    
    // Update is called once per frame
    void Update () {
        
    }
 
    public void setMoney()
    {
        // 가진 돈 출력
        GameObject MoneyImage = GameObject.FindGameObjectWithTag("Money");
        Vector2 moneyPos = new Vector2(0.8f, 0.65f);
        MoneyImage.GetComponent<ImageScore>().printScore(money, moneyPos, 'L'true);
    }
 
    public void MoneyUp(int plus)
    {
        money += plus;
        setMoney();
    }
 
    public int getMoney()
    {
        return money;
    }
 
    public void MoneyMinus(int minus)
    {
        money -= minus;
        setMoney();
    }
}
cs


몬스터는 죽을때 MoneyUp 함수를 불러준다.

1
GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>().MoneyUp(50);
cs





타워를 생성할때에는 이제 골드를 체크하여 골드가 없으면 지어지지않게.

골드가 있으면 골드를 소모하고 타워가 설치되도록 하였다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// 실제 타워 생성
if (BuildAllow)
{
    bool towergold = false;
    if (gameObject.tag == "Tower1")
    {
        if (GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>().getMoney() >= 150)
        {
            towergold = true;
            GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>().MoneyMinus(150);
        }
    }
    else if (gameObject.tag == "Tower2")
    {
        if (GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>().getMoney() >= 200)
        {
            towergold = true;
            GameObject.FindGameObjectWithTag("GameController").GetComponent<GameController>().MoneyMinus(200);
        }
    }
 
    if (towergold)
        Instantiate(realTower, Instantpos, Quaternion.identity);
}
cs




실행화면

td_hp_gold.mp4



이제 몬스터의 HP바도 잘 뜨고 골드증가 감소도 정상적으로 잘 동작한다.

Posted by misty_
,