본문 바로가기
Project/<team Not Same> 꿈의 왕국 : 영원한 보금자리

[R&D] UI small Stage Map UIPlayer Move

by 왹져박사 2023. 5. 1.
728x90

UI상의 Stage Map에서 UIPlayer가 선택한 맵으로 이동하는 연출 (레퍼런스 : 프로즌시티)을 표현하고 싶었다. 

처음에는, UIStagePlayer의 CMove 코루틴을 목표를 position으로 잡았더니 어떤 경우에는 멈추기도 하고 어떤 경우에는 멈추지 않아 계속해서 코루틴이 중복되는 현상이 발생하였다. 

 그런 고민 중에 추천영상으로 뜬 오늘코딩 채널의 강의를 보고 문제를 해결하였다. 

https://youtu.be/_QOvSLCXm7A

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

public class UIMap : MonoBehaviour
{
    public Button btnMap;
    public UIStage uiStage;
    public UIKingdomStage uiKingdomStage;

    void Start()
    {
        this.uiStage.gameObject.SetActive(false);
        this.uiKingdomStage.gameObject.SetActive(false);

        this.btnMap.onClick.AddListener(() =>
        {
            Debug.Log("btnMap clicked");
            this.uiStage.gameObject.SetActive(true);
        });
        this.uiStage.onClickBack = () =>
        {
            Debug.Log("onClickBack uiStage UIMap");
            this.uiStage.gameObject.SetActive(false);
            this.uiKingdomStage.gameObject.SetActive(true);
        };
        this.uiKingdomStage.onClickBack = () =>
        {
            Debug.Log("onClickBack uiKingdomStage UIMap");
            this.uiKingdomStage.gameObject.SetActive(false);
        };
    }

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

public class UIStage : MonoBehaviour
{
    public Button btnBack;
    public UIStageCell[] uiStageCells;
    public UIStagePlayer uiStagePlayer;

    public UIStageCell nowStage;

    public System.Action onClickBack;

    void Start()
    {
        this.uiStagePlayer.transform.position = this.nowStage.pos.position;
        this.btnBack.onClick.AddListener(() =>
        {
            Debug.Log("btnBack clicked");
            this.onClickBack();
        });

        foreach (UIStageCell uiStageCell in this.uiStageCells)
        {
            uiStageCell.btnStage.onClick.AddListener(() =>
            {
                Debug.LogFormat("{0} clicked", uiStageCell.name);
                if (!uiStagePlayer.isMoving)
                {
                    this.SelectStage(uiStageCell);
                }
            });
        }
    }

    public void SelectStage(UIStageCell selectStage)
    {
        Debug.LogFormat("{0} selected", selectStage);
        this.nowStage.focus.SetActive(false);
        selectStage.focus.SetActive(true);
        Debug.LogFormat("targetStageCell : {0}", selectStage);
        this.uiStagePlayer.MoveOn(selectStage.pos);
        this.nowStage = selectStage;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIStagePlayer : MonoBehaviour
{
    public float speed = 5f;
    public bool isMoving = false;

    public void MoveOn(Transform targetTrans)
    {
        Debug.Log("UIStagePlayer Move");
        this.StartCoroutine(this.CMove(targetTrans));
    }

    public IEnumerator CMove(Transform targetTrans)
    {
        Debug.Log("UIStagePlayer CMove");
        this.isMoving = true;
        Vector3 startPos = this.transform.position;

        float distance = Vector3.Distance(startPos, targetTrans.position);
        float totlaTime = distance / speed;
        float time = 0f;

        while (time < totlaTime)
        {
            time += Time.deltaTime;
            float t = Mathf.Clamp01(time / totlaTime);

            this.transform.position = Vector3.Lerp(startPos, targetTrans.position, t);

            yield return null;
        }
        this.isMoving = false;
        Debug.Log("<color=yellow>Move done</color>");
    }
}

728x90