본문 바로가기
개발/게임) 프로토타입

게임개발) 코인 연출(흩뿌리고 타겟 이동)

by 테샤르 2020. 6. 16.

코인 연출(흩뿌리고 타깃 이동)

코인을 연출을 하는 과정에서 사용자가 코인에 대해서 풍부함을 느끼게 하기 위해서 

생성된 위치를 기반으로 흩뿌리고 일정 시간 이후에 코인 UI의 심벌로 타깃 이동(가속도) 처리를 진행했다.

반응형

 

사용된 코드는 대략 다음과 같다.

  public void UpdateCoinAnimation(GameObject _start, GameObject _end, int _animationCount)
        {
            this.m_nCoinAnimationCount = _animationCount;
            for (int i = 0; i < this.m_CoinAinamationItemList.Count; i++)
            {
                if(_animationCount <= i){
                    continue;
                }

                this.m_CoinAinamationItemList[i].Initalize(new Vector3(Random.Range(-200,200f),Random.Range(-200,200f),0f), _start, _end);
            }
        }

 


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

namespace UI
{

    public enum eCOIN_ANIMATION_STATE
    {
        NONE,
        INIT,
        ANIMATION_1,
        ANIMATION_2,
        END,
    }

    public class UICoinAnimationItem : MonoBehaviour
    {

        private GameObject m_GObjStart = null;
        private GameObject m_GObjTarget = null;

        private RollingAccumulate m_RollingValue = null;
        private Vector3 m_vRandomTarget = Vector2.zero;
        private Vector3 m_vTarget = Vector2.zero;

        private eCOIN_ANIMATION_STATE m_eState = eCOIN_ANIMATION_STATE.NONE;
        private RectTransform m_RectTransform = null;

        #region Unity Method

        void Update()
        {
            if (null != this.m_RollingValue)
            {
                if (false == this.m_RollingValue.GetComplete())
                {
                    this.m_RollingValue.Update(Time.deltaTime);
                    if (true == this.m_RollingValue.GetComplete())
                    {
                        this.SetState(this.m_eState + 1);
                    }
                }
                Vector3 pos = Vector3.zero;
                switch (this.m_eState)
                {
                    case eCOIN_ANIMATION_STATE.INIT:
                        {
                            pos = Vector3.Lerp(this.transform.localPosition, this.m_vTarget, this.m_RollingValue.GetCurrentValue());
                            this.SetPosition(pos);
                        }
                        break;
                    case eCOIN_ANIMATION_STATE.ANIMATION_1:
                        {
                            pos = Vector3.Lerp(this.transform.localPosition, this.m_vTarget, this.m_RollingValue.GetCurrentValue());
                            this.SetPosition(pos);
                        }
                        break;
                    case eCOIN_ANIMATION_STATE.ANIMATION_2:
                        {
                            pos = Vector3.Lerp(this.transform.localPosition, this.m_vTarget, this.m_RollingValue.GetCurrentValue());
                            this.SetPosition(pos);
                        }
                        break;
                }
            }
        }

        #endregion

        #region Private Method

        private void SetState(eCOIN_ANIMATION_STATE _state)
        {
            this.m_eState = _state;

            this.m_RollingValue.SetValue(0f, 1f, 0.3f, null, null);

            switch (_state)
            {
                case eCOIN_ANIMATION_STATE.INIT:
                    {
                        
                        this.SetActive(true);
                        this.m_vTarget = this.m_vRandomTarget;
                    }
                    break;
                case eCOIN_ANIMATION_STATE.ANIMATION_1:
                    {
                        this.m_vTarget = this.m_GObjTarget.transform.localPosition;
                    }
                    break;
                case eCOIN_ANIMATION_STATE.ANIMATION_2:
                    {
                        this.m_vTarget = this.m_GObjTarget.transform.localPosition;
                    }
                    break;
                case eCOIN_ANIMATION_STATE.END:
                    {
                        this.SetActive(false);
                    }
                    break;
            }
        }


        public bool GetFinish()
        {
            return (this.m_eState == eCOIN_ANIMATION_STATE.END);
        }

        private Vector2 GetConverVec2(Vector3 _pos){
            Vector3 pos = Camera.main.WorldToViewportPoint(_pos);
            return new Vector3(_pos.x, _pos.y, 0f);
        }

        private void SetPosition(Vector2 _pos)
        {
            this.m_RectTransform.localPosition = new Vector3(_pos.x, _pos.y, -500f);
        }

        private void SetActive(bool _active)
        {
            this.gameObject.SetActive(_active);
        }

        #endregion
        #region Public Method

        public void Initalize(Vector3 _target, GameObject _start, GameObject _end)
        {

            if (null == this.m_RollingValue)
            {
                this.m_RollingValue = new RollingAccumulate();
            }

            if (null == this.m_RectTransform)
            {
                this.m_RectTransform = this.GetComponent<RectTransform>();
            }

            this.m_vRandomTarget =_target;
            this.m_GObjStart = _start;
            this.m_GObjTarget = _end;
            
            this.SetPosition(this.GetConverVec2(this.m_GObjStart.transform.position));
           

            this.SetState(eCOIN_ANIMATION_STATE.INIT);
        }

        #endregion
    }

}

 단순하게 코인이 타깃 이동하는 것보다는 흩뿌리고 난 이후에 이동을 하게 되니 조금 더 풍부해지고 코인이 증가한다는 느낌이 강해져서 상황에 따라는 좋은 것 같다.

반응형

댓글