Пример #1
0
    /// <summary>
    /// Plays the movement to follow the path.
    /// </summary>
    /// <param name="forward"> A boolean to make it go forward or backwards.</param>
    private void Play(bool forward = true)
    {
        switch (speedMode)                          // Choose between using speed or time
        {
        default:

        case SpeedMode.ConstantSpeed:               // Make it to move at a constant speed on every node
            float m = (rail.nodes[currentSeg + 1].position - rail.nodes[currentSeg].position).magnitude;
            float s = (Time.deltaTime * 1 / m) * speed;
            transition += (forward) ? s : -s;
            break;

        case SpeedMode.TimeBetweenSegments:         // Make the object to reach next node in timeBetweenSegments
            float move = Time.deltaTime * 1 / timeBetweenSegments;
            transition += (forward) ? move : -move;
            break;
        }
        if (transition > 1 && !loopNode)             // Already reached next segment and it doesn't want to loop over same node.
        {
            transition = 0;                          // Restart the count.
            currentSeg++;                            // Update the segment it is in.
            if (currentSeg == rail.nodes.Length - 1) // Last segment reached.
            {
                if (isLooping)                       // If it is looping start again or play it backwards (pingPong).
                {
                    if (pingPong)
                    {
                        transition = 1;
                        currentSeg = rail.nodes.Length - 2;
                        isReversed = !isReversed;
                    }
                    else
                    {
                        currentSeg = 0;
                    }
                }
                else
                {
                    isCompleted = true;
                }
            }
        }
        else if (transition > 1 && loopNode)    // Already reached next segment and want to loop over same node.
        {
            transition = 0;                     // Restart the count.
        }
        else if (transition < 0 && !loopNode)   // Same logic that before but backwards, reached next segment and it doesn't want to loop over same node.
        {
            transition = 1;                     // Restart the count.
            currentSeg--;                       // Update the segment it is in.
            if (currentSeg == -1)               // Last segment reached.
            {
                if (isLooping)                  // If it is looping start again or play it backwards (pingPong).
                {
                    if (pingPong)
                    {
                        transition = 0;
                        currentSeg = 0;
                        isReversed = !isReversed;
                    }
                    else
                    {
                        currentSeg = rail.nodes.Length - 2;
                    }
                }
            }
        }
        else if (transition < 0 && loopNode) // Same logic that before but backwards, reached next segment and it wants to loop over same node.
        {
            transition = 1;                  // Restart the count
        }
        // Update position and orientation.
        if (playMode != PlayMode.Catmull || currentSeg < rail.nodes.Length - 1)
        {
            transform.position = rail.PositionOnRail(currentSeg, transition, playMode);
        }
        if (orientationMode != OrientationMode.None)
        {
            transform.rotation = rail.OrientationOnRail(currentSeg, transition, orientationMode, transform, isReversed);
        }
    }