unity学习笔记,在格子上交换两个OBJECT的动画

对于object自己来讲,有一个move的函数。

public void Move(int destX,int destY,float timeToMove)
    {
        if (!m_isMoving)
        {
            StartCoroutine(MoveRoutine(new Vector3(destX, destY, 0), timeToMove));
        }
    }
    IEnumerator MoveRoutine(Vector3 destination, float timeToMove)
    {
        Vector3 startPosition = transform.position;
        bool reachDestination = false;
        float elapsedTime = 0f;
        m_isMoving = true;
        while (!reachDestination)
        {
            if (Vector3.Distance(transform.position, destination) < 0.01)
            {
                reachDestination = true;                if (m_board != null)
                {
                    m_board.PlaceGamePiece(this, (int)destination.x, (int)destination.y);
                }
                //transform.position = destination;
                //SetCoord((int)destination.x, (int)destination.y);
                break;
            }            elapsedTime += Time.deltaTime;
            float t = Mathf.Clamp(elapsedTime / timeToMove,0f,1f);            switch (interPolation)
            {
                case InterpType.Linear:
                    break;
                case InterpType.EaseOut:
                    t = Mathf.Sin(t * Mathf.PI * 0.5f);
                    break;
                case InterpType.EaseIn:
                    t = 1 - Mathf.Cos(t * Mathf.PI * 0.5f);
                    break;
                case InterpType.SmoothStep:
                    t = t * t * (3 - 2 * t);
                    break;
                case InterpType.SmootherStep:
                    t = t * t * t * (t * (t * 6 - 15) + 10);
                    break;
            }            transform.position = Vector3.Lerp(startPosition, destination, t);            //wait until next time
            yield return null;
        }
        m_isMoving = false;
    }

这个move的函数在何时调用?在我们在格子面板上触发交换时调用。

  void SwitchTiles(Tile clickTile, Tile targetTile)
    {
        GamePiece clickedPiece = m_allGamePieces[clickTile.xIndex, clickTile.yIndex];
        GamePiece targetPiece = m_allGamePieces[targetTile.xIndex, targetTile.yIndex];
        clickedPiece.Move(targetTile.xIndex, targetTile.yIndex, swapTime);
        targetPiece.Move(clickTile.xIndex, clickTile.yIndex, swapTime);
        m_clickTile = null;
        m_targetTile = null;
    }

以上内容摘自udemy Make a Match-Three Puzzle Game in Unity课程~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值