コード例 #1
0
    void FixedUpdate()
    {
        Vector3 oldPos = MyTransform.position;

        if (Current != null && (Current.Next != null || Current.Previous != null))
        {
            //Move towards the next node.
            Vector3 newPos;
            float moveDist = BaseSpeed * Time.fixedDeltaTime;
            if (Current.MoveTowardsNext(MyTransform.position, moveDist, MovingBackwards, out newPos))
            {
                //First, get the next target node.
                Current = Current.GetNextNode(MovingBackwards);
                bool hitEnd = Current.GetNextNode(MovingBackwards) == null;
                if (hitEnd)
                {
                    MovingBackwards = !MovingBackwards;
                    if (OnPathEnd != null) OnPathEnd(this, new System.EventArgs());
                }

                //Since this cube just snapped to a new node, there may be a bit of movement left.
                if (!hitEnd)
                {
                    float extraMovement = (moveDist / Current.SpeedModifier) -
                                          Vector3.Distance(MyTransform.position, newPos);
                    if (extraMovement > 0.0f)
                    {
                        Vector3 newNewPos;
                        Current.MoveTowardsNext(newPos, extraMovement, MovingBackwards, out newNewPos);
                        MyTransform.position = newNewPos;
                    }
                }
            }
            else
            {
                MyTransform.position = newPos;
            }
        }

        Velocity = (MyTransform.position - oldPos) / Time.deltaTime;
    }