실행 이미지

 

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

public class HitDamageText : MonoBehaviour
{
    private TMP_Text damageText;

    void Start()
    {
        this.damageText = this.GetComponent<TMP_Text>();

        // 랜덤한 Z축 회전 각도
        float randZoffset = Random.Range(-30.0f, 30.0f);

        // Z축을 기준으로 랜덤한 각도로 회전
        this.transform.Rotate(0, 0, randZoffset);

        // 이동해야 할 총 거리
        float moveDistance = 200f;

        // 회전된 객체의 위쪽 벡터를 사용하여 실제 이동 방향 결정
        Vector3 direction = this.transform.up * moveDistance;

        // 현재 위치에서 방향과 거리를 고려한 목표 위치 계산
        Vector3 targetPosition = this.transform.localPosition + direction;

        // 수정된 목표 위치로 이동
        this.transform.DOLocalMove(targetPosition, 1).OnComplete(() => {
            Debug.Log("Move Complete!");
            Destroy(this.gameObject);
        });

        this.transform.DOScale(2, 0.5f).OnComplete(() =>
        {
            Debug.Log("DoScale Complete 1 -> 2");
            this.transform.DOScale(0.1f, 0.5f).OnComplete(() => {
                Debug.Log("DoScale Complete 2 -> 0.1");
            });

            var tmpUGUI = this.damageText.GetComponent<TextMeshProUGUI>();
            DOTween.ToAlpha(() => tmpUGUI.color, x => tmpUGUI.color = x, 0, 0.5f);
        });
    }
}

HitDamageText.cs

 

(Hit Damage Text 프리팹에 부착할 스크립트)

 

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

public class UIAnimationMain : MonoBehaviour
{
    [SerializeField] private Transform point;   //보스의 게이지 위치 (월드상)
    [SerializeField] private Transform bossGauge;
    [SerializeField] private Canvas canvas;

    //[SerializeField] private TMP_Text hitDamageText;
    [SerializeField] private Transform hitPoint;
    [SerializeField] private TMP_Text hitDamageTextPrefab;
    [SerializeField] private GameObject hitEffectPrefab;

  

    void Update()
    {
        //Canvas 의 Render Mode가 Overlay일경우 
        //월드 위치를 스크린 위치로 변환 
        Vector2 screenPosition = Camera.main.WorldToScreenPoint(point.position);

        //스크린 좌표를 -> Canvas의 RectTransform로컬좌표로 변환 
        RectTransform canvasRectTransform = canvas.GetComponent<RectTransform>();
        Vector2 canvasLocalPosition;

        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform, screenPosition, null, out canvasLocalPosition);

        Debug.Log(canvasLocalPosition);
        bossGauge.localPosition = canvasLocalPosition;

        if (Input.GetMouseButtonDown(0))
        {
            StartCoroutine("HitDamageTextCreate");
        }

    }

    IEnumerator HitDamageTextCreate()
    {
        Vector2 screenPosition2 = Camera.main.WorldToScreenPoint(hitPoint.position);

        //스크린 좌표를 -> Canvas의 RectTransform로컬좌표로 변환 
        RectTransform canvasRectTransform2 = canvas.GetComponent<RectTransform>();
        Vector2 canvasLocalPosition2;

        RectTransformUtility.ScreenPointToLocalPointInRectangle(canvasRectTransform2, screenPosition2, null, out canvasLocalPosition2);

        Debug.Log(canvasLocalPosition2);

        TMP_Text hitGO = Instantiate(hitDamageTextPrefab, transform);
        hitGO.text = Random.Range(0, 1000) + " Hit!";
        hitGO.transform.localPosition = canvasLocalPosition2;

        yield return null;
    }
}

UIAnimationMain.cs

 

(Canvas에 부착할 스크립트)

+ Recent posts