Unity/유니티 기본

[4.30] 데미지가 지정된 위치에 출력되지 않는 문제

ljw4104 2021. 4. 30. 13:09

원래 계획했던 데미지텍스트가 출력되는 위치 / 실제로 캔버스 상에서 출력되는 위치

============================================================================

*해결*

코드를 좀 수정하니 됐다.

보스를 잡을 때마다 데미지를 출력 및 아이템 드랍

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

public class Boss : MonoBehaviour
{
    public enum eStatus
    {
        Idle, Damaged, Die
    }

    public Transform target;
    public Transform damageTarget;
    public RectTransform canvas;
    public RectTransform hpBar;
    public GameObject hitFx;
    public GameObject labelPrefab;

    [SerializeField]
    private float hp;
    private float maxHp;
    private Rigidbody bossRigidbody;
    private Animator anim;
    private eStatus status;
    // Start is called before the first frame update
    void Start()
    {
        this.hp = this.maxHp = 300;
        this.bossRigidbody = GetComponent<Rigidbody>();
        this.anim = GetComponent<Animator>();

        this.hpBar.gameObject.SetActive(false);
        Test.Instance.onBossCall += () =>
        {
            this.bossRigidbody.useGravity = true;
            this.hpBar.gameObject.SetActive(true);
        };
    }

    // Update is called once per frame
    void Update()
    {
        this.DetachUIToWorld(this.target, this.hpBar);
        if (this.hp <= 0 && this.status != eStatus.Die)
        {
            this.status = eStatus.Die;
            this.anim.SetTrigger("die");
            this.hpBar.gameObject.SetActive(false);
            Test.Instance.onDropItem();
            Destroy(gameObject, 4f);
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (Test.Instance.player.state == JoystickTest.eState.Attack && this.status != eStatus.Damaged)
            if (other.CompareTag("Weapon"))
            {
                var go = Instantiate(this.hitFx.GetComponent<ParticleSystem>(), this.transform.position, Quaternion.identity);
                go.Play();

                GameObject lab = Instantiate(this.labelPrefab);
                lab.transform.SetParent(canvas.transform);
                Vector3 screenPos = Camera.main.WorldToScreenPoint(this.damageTarget.position);
                lab.transform.position = screenPos;
                lab.GetComponent<Text>().text = other.GetComponent<Sword>().power.ToString();
                lab.transform.DOLocalMoveY(lab.transform.localPosition.y + 50, 0.5f).SetEase(Ease.OutQuad).onComplete = () =>
                  {
                      Destroy(lab);
                  };
                this.hp -= other.GetComponent<Sword>().power;
                if (this.hp > 0)
                {
                    StartCoroutine(this.Damaged());
                    this.hpBar.gameObject.GetComponent<Slider>().value = this.hp / this.maxHp;
                }
            }
    }

    private IEnumerator Damaged()
    {
        this.status = eStatus.Damaged;
        this.anim.SetBool("damaged", true);
        yield return new WaitForSeconds(0.667f);
        this.anim.SetBool("damaged", false);
        this.status = eStatus.Idle;
    }

    private void DetachUIToWorld(Transform target, RectTransform obj)
    {
        Vector3 offsetPos = target.transform.position;
        Vector2 screenPoint = Camera.main.WorldToScreenPoint(offsetPos);
        Vector2 canvasPos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas, screenPoint, null, out canvasPos);

        obj.localPosition = canvasPos;
    }
}

 

 

이제 구현해야 할 것

  1. 데이터 테이블 작성 (유저모델, 몬스터, 아이템)
  2. 유저는 잡몬스터를 잡고 일정 수준이상 잡으면 보스 몬스터가 하늘에서 떨어짐
  3. 잡몬스터는 공격을 하지 않지만 보스몬스터는 공격을 함.
  4. 보스의 공격패턴 만들기
  5. 유저의 hp표시, 인벤토리 UI 제작하기