Beispiel #1
0
        private void FixedUpdate()
        {
            //注:Time.deltaTime在FixUpdate()内是固定时间,取决于物理系统设置
            if (!m_Started)
            {
                return;
            }

            //no need to update we have a single node in the path
            if (m_Current == m_Next)
            {
                return;
            }

            if (m_WaitTime > 0)
            {
                m_WaitTime -= Time.deltaTime;
                return;
            }

            float distanceToGo = speed * Time.deltaTime;

            //当需要移动的距离大于0
            while (distanceToGo > 0)
            {
                Vector2 direction = m_WorldNode[m_Next] - transform.position;

                float dist = distanceToGo;
                //当 离下一个节点的距离 小于 将要移动的位移时,即继续执行会超出目标点时
                if (direction.sqrMagnitude < dist * dist)
                {   //we have to go farther than our current goal point, so we set the distance to the remaining distance
                    //then we change the current & next indexes
                    dist = direction.magnitude;

                    m_Current = m_Next;

                    m_WaitTime = waitTimes[m_Current];

                    if (m_Dir > 0)
                    {
                        m_Next += 1;
                        if (m_Next >= m_WorldNode.Length)
                        { //we reach the end
                            switch (platformType)
                            {
                            case MovingPlatformType.BACK_FORTH:
                                m_Next = m_WorldNode.Length - 2;
                                m_Dir  = -1;
                                break;

                            case MovingPlatformType.LOOP:
                                m_Next = 0;
                                break;

                            case MovingPlatformType.ONCE:
                                m_Next -= 1;
                                StopMoving();
                                break;
                            }
                        }
                    }
                    else
                    {
                        m_Next -= 1;
                        if (m_Next < 0)
                        { //reached the beginning again
                            switch (platformType)
                            {
                            case MovingPlatformType.BACK_FORTH:
                                m_Next = 1;
                                m_Dir  = 1;
                                break;

                            case MovingPlatformType.LOOP:
                                m_Next = m_WorldNode.Length - 1;
                                break;

                            case MovingPlatformType.ONCE:
                                m_Next += 1;
                                StopMoving();
                                break;
                            }
                        }
                    }
                }

                //移动的距离+方向
                m_Velocity = direction.normalized * dist;

                //transform.position +=  direction.normalized * dist;
                m_Rigidbody2D.MovePosition(m_Rigidbody2D.position + m_Velocity);
                platformCatcher.MoveCaughtObjects(m_Velocity);
                //We remove the distance we moved. That way if we didn't had enough distance to the next goal, we will do a new loop to finish
                //the remaining distance we have to cover this frame toward the new goal
                //这里利用浮点数减法的结果判断是否大于0来执行while循环,有待商榷
                //Debug.Log(distanceToGo + "-" + dist + "=" + (distanceToGo - dist));
                distanceToGo -= dist;
                // we have some wait time set, that mean we reach a point where we have to wait. So no need to continue to move the platform, early exit.
                //跳出,执行等待
                if (m_WaitTime > 0.001f)
                {
                    break;
                }
            }
        }
Beispiel #2
0
        private void FixedUpdate()
        {
            if (!m_Started)
            {
                return;
            }

            //no need to update we have a single node in the path
            if (m_Current == m_Next)
            {
                return;
            }

            if (m_WaitTime > 0)
            {
                m_WaitTime -= Time.deltaTime;
                return;
            }

            var distanceToGo = speed * Time.deltaTime;

            while (distanceToGo > 0)
            {
                Vector2 direction = m_WorldNode[m_Next] - transform.position;

                var dist = distanceToGo;
                if (direction.sqrMagnitude < dist * dist)
                {
                    //we have to go farther than our current goal point, so we set the distance to the remaining distance
                    //then we change the current & next indexes
                    dist = direction.magnitude;

                    m_Current = m_Next;

                    m_WaitTime = waitTimes[m_Current];

                    if (m_Dir > 0)
                    {
                        m_Next += 1;
                        if (m_Next >= m_WorldNode.Length)
                        {
                            switch (platformType)
                            {
                            case MovingPlatformType.BACK_FORTH:
                                m_Next = m_WorldNode.Length - 2;
                                m_Dir  = -1;
                                break;

                            case MovingPlatformType.LOOP:
                                m_Next = 0;
                                break;

                            case MovingPlatformType.ONCE:
                                m_Next -= 1;
                                StopMoving();
                                break;
                            }
                        }
                    }
                    else
                    {
                        m_Next -= 1;
                        if (m_Next < 0)
                        {
                            switch (platformType)
                            {
                            case MovingPlatformType.BACK_FORTH:
                                m_Next = 1;
                                m_Dir  = 1;
                                break;

                            case MovingPlatformType.LOOP:
                                m_Next = m_WorldNode.Length - 1;
                                break;

                            case MovingPlatformType.ONCE:
                                m_Next += 1;
                                StopMoving();
                                break;
                            }
                        }
                    }
                }

                m_Velocity = direction.normalized * dist;

                //transform.position +=  direction.normalized * dist;
                m_Rigidbody2D.MovePosition(m_Rigidbody2D.position + m_Velocity);
                platformCatcher.MoveCaughtObjects(m_Velocity);
                //We remove the distance we moved. That way if we didn't had enough distance to the next goal, we will do a new loop to finish
                //the remaining distance we have to cover this frame toward the new goal
                distanceToGo -= dist;

                // we have some wait time set, that mean we reach a point where we have to wait. So no need to continue to move the platform, early exit.
                if (m_WaitTime > 0.001f)
                {
                    break;
                }
            }
        }