Пример #1
0
    public Vector2 getSteering(LinePath path)
    {
        Vector2 targetPosition;

        // If the path has only one node then just go to that position;
        if (path.Length == 1)
        {
            targetPosition = path[0];
        }
        // Else find the closest spot on the path to the character and go to that instead.
        else
        {
            /* Find the final destination of the character on this path */
            Vector2 finalDestination = (pathDirection > 0) ? path[path.Length - 1] : path[0];

            /* If we are close enough to the final destination then either stop moving or reverse if
             * the character is set to loop on paths */
            if (Vector2.Distance(transform.position, finalDestination) < stopRadius)
            {
                if (pathLoop)
                {
                    pathDirection *= -1;
                }
                else
                {
                    rb.velocity = Vector2.zero;
                    return(Vector2.zero);
                }
            }

            /* Get the param for the closest position point on the path given the character's position */
            float param = path.getParam(transform.position);

            /* Move down the path */
            param += pathDirection * pathOffset;

            /* Make sure we don't move past the beginning or end of the path */
            if (param < 0)
            {
                param = 0;
            }
            else if (param > path.maxDist)
            {
                param = path.maxDist;
            }

            /* Set the target position */
            targetPosition = path.getPosition(param);
        }

        return(steeringUtils.arrive(targetPosition));
    }
Пример #2
0
    void updatePathParam()
    {
        float param;

        if (currentPath.Length > 1)
        {
            param = currentPath.getParam(transform.position);
        }
        else
        {
            param = Vector2.Distance(transform.position, currentPath [0]);
        }

        if (param > farthestPathParam)
        {
            farthestPathParam = param;
            farthestPathTime  = Time.time;
        }
    }
Пример #3
0
    /// <summary>
    /// Will return true if the character is at the end of the given path
    /// </summary>
    public bool isAtEndOfPath(LinePath path)
    {
        // If the path has only one node then just check the distance to that node
        if (path.Length == 1)
        {
            Vector3 endPos = rb.convertVector(path[0]);
            return(Vector3.Distance(rb.position, endPos) < stopRadius);
        }
        // Else see if the character is at the end of the path
        else
        {
            Vector3 finalDestination;

            /* Get the param for the closest position point on the path given the character's position */
            float param = path.getParam(transform.position, rb);

            return(isAtEndOfPath(path, param, out finalDestination));
        }
    }
Пример #4
0
    public Vector3 getSteering(LinePath path, bool pathLoop, out Vector3 targetPosition)
    {
        // 如果路径只有一个节点, 那么只需转到该位置
        if (path.Length == 1)
        {
            targetPosition = path[0];
        }
        //否则, 在该路径上找到最接近的点, 然后转到该位置。
        else
        {
            if (!pathLoop)
            {
                /* 查找此路径上的 最终目标 */
                Vector2 finalDestination = (pathDirection > 0) ? path[path.Length - 1] : path[0];

                // 如果我们足够接近最终目的地,  就停止

                /* If we are close enough to the final destination then either stop moving or reverse if
                 * the character is set to loop on paths */
                if (Vector2.Distance(transform.position, finalDestination) < stopRadius)
                {
                    targetPosition = finalDestination;

                    rb.velocity = Vector2.zero;
                    return(Vector2.zero);
                }
            }

            /* 在给定路径上,得到最接近的位置点的参数*/
            float param = path.getParam(transform.position);

            /* 向下移动的路径 */
            param += pathDirection * pathOffset;

            /* 得到目标位置 */
            targetPosition = path.getPosition(param, pathLoop);
        }

        return(steeringBasics.Arrive(targetPosition));
    }
Пример #5
0
    public Vector3 getSteering(LinePath path, bool pathLoop, out Vector3 targetPosition)
    {
        // If the path has only one node then just go to that position;
        if (path.Length == 1)
        {
            targetPosition = path[0];
        }
        // Else find the closest spot on the path to the character and go to that instead.
        else
        {
            /* Get the param for the closest position point on the path given the character's position */
            float param = path.getParam(transform.position, rb);

            //Debug.DrawLine(transform.position, path.getPosition(param, pathLoop), Color.red, 0, false);

            if (!pathLoop)
            {
                Vector3 finalDestination;

                /* If we are close enough to the final destination then stop moving */
                if (isAtEndOfPath(path, param, out finalDestination))
                {
                    targetPosition = finalDestination;

                    rb.velocity = Vector3.zero;
                    return(Vector3.zero);
                }
            }

            /* Move down the path */
            param += pathDirection * pathOffset;

            /* Set the target position */
            targetPosition = path.getPosition(param, pathLoop);

            //Debug.DrawLine(transform.position, targetPosition, Color.red, 0, false);
        }

        return(steeringBasics.arrive(targetPosition));
    }
Пример #6
0
    public Vector3 getSteering(LinePath path, bool pathLoop, out Vector3 targetPosition)
    {
        // If the path has only one node then just go to that position;
        if (path.Length == 1)
        {
            targetPosition = path[0];
        }
        // Else find the closest spot on the path to the character and go to that instead.
        else
        {
            if (!pathLoop)
            {
                /* Find the final destination of the character on this path */
                Vector2 finalDestination = (pathDirection > 0) ? path[path.Length - 1] : path[0];

                /* If we are close enough to the final destination then either stop moving or reverse if
                 * the character is set to loop on paths */
                if (Vector2.Distance(transform.position, finalDestination) < stopRadius)
                {
                    targetPosition = finalDestination;

                    rb.velocity = Vector2.zero;
                    return(Vector2.zero);
                }
            }

            /* Get the param for the closest position point on the path given the character's position */
            float param = path.getParam(transform.position);

            /* Move down the path */
            param += pathDirection * pathOffset;

            /* Set the target position */
            targetPosition = path.getPosition(param, pathLoop);
        }

        return(steeringBasics.arrive(targetPosition));
    }
    public Vector2 getSteering(LinePath path, bool pathLoop, out Vector2 targetPosition)
    {
        // If the path has only one node then just go to that position;
        if (path.Length == 1) {
            targetPosition = path[0];
        }
        // Else find the closest spot on the path to the character and go to that instead.
        else {
            /* Find the final destination of the character on this path */
            Vector2 finalDestination = (pathDirection > 0) ? path[path.Length-1] : path[0];

            /* If we are close enough to the final destination then either stop moving or reverse if
             * the character is set to loop on paths */
            if( Vector2.Distance(transform.position, finalDestination) < stopRadius ) {
                if(pathLoop) {
                    pathDirection *= -1;
                } else {
                    targetPosition = finalDestination;

                    rb.velocity = Vector2.zero;
                    return Vector2.zero;
                }
            }

            /* Get the param for the closest position point on the path given the character's position */
            float param = path.getParam(transform.position);

            /* Move down the path */
            param += pathDirection * pathOffset;

            /* Set the target position */
            targetPosition = path.getPosition(param);
        }

        return steeringUtils.arrive(targetPosition);
    }