예제 #1
0
        /// <summary>
        /// Appends an input path to the end of this path if the start point of the input
        /// path and end point of this path are the same
        /// </summary>
        /// <param name="path">The path to append to this path</param>
        /// <returns>True if the append was successful. False otherwise.</returns>
        public bool AppendPathToEnd(Path path)
        {
            if (!IsGenerated() || !path.IsGenerated())
            {
                Debug.LogWarning("Could not append paths because one of the paths is not generated");
                return(false);
            }
            List <Vector3Int> newPoints = path.GetPathPointList();

            if (newPoints.Count == 0)
            {
                Debug.LogWarning("Could not append paths because the new end points have no values");
                return(false);
            }
            if (newPoints[0] != pathpoints[pathpoints.Count - 1])
            {
                Debug.LogError("Could not append paths because the old end point and new start point are not equal");
                return(false);
            }
            for (int i = 1; i < newPoints.Count; i++)
            {
                pathpoints.Add(newPoints[i]);
            }
            if (pathLength > -1)
            {
                pathLength = -1;
                GetPathLength();
            }
            return(true);
        }
예제 #2
0
    void movement()
    {
        if (path.IsGenerated())
        {
            if (isStationary)
            {
                //Get the next point in the path as world position. returns true when successful
                if (path.GetNextPoint(ref nextPoint))
                {
                    //face direction
                    Vector3 difference = nextPoint - transform.position;
                    float   rotationZ  = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
                    transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);

                    //Move
                    rid.velocity  = nextPoint - transform.position;
                    rid.velocity  = rid.velocity.normalized;
                    rid.velocity *= speed;
                    isStationary  = false;
                }
                else     //When GetNextPoint fails it means we have reached the end of the path
                {
                    rid.velocity = Vector3.zero;
                    isStationary = true;
                }
            }
            else
            {
                Vector3 delta = nextPoint - transform.position;
                if (delta.magnitude <= 0.2f)
                {
                    rid.velocity = Vector3.zero;
                    isStationary = true;
                }
            }
        }
        else
        {
            rid.velocity = Vector3.zero;
            isStationary = true;
        }
    }