/// <summary>
    /// Checks if the next waypoint is positioned on the ground and can thus be walked on
    /// </summary>
    /// <param name="terrainLayers"></param>
    /// <returns></returns>
    public bool IsWaypointOnGround(LayerMask terrainLayers)
    {
        bool isOnGround;


        isOnGround = Physics2D.Raycast(path.vectorPath[currentWaypoint], Vector3.down, 1.5f, terrainLayers).collider != null;


        if (ai.showDetectors)
        {
            DebugDrawPhysics.DebugDrawCircle(path.vectorPath[currentWaypoint], 0.25f, isOnGround ? Color.green : Color.red, 1f / ai.checksPerSecond);
        }

        return(isOnGround);
    }
Beispiel #2
0
        public bool IsWaypointOnGround(LayerMask terrainLayers)
        {
            bool isOnGround;

            if (is2D)
            {
                isOnGround = Physics2D.Raycast(path.vectorPath[currentWaypoint], Vector3.down, 1.5f, terrainLayers).collider != null;
            }
            else
            {
                isOnGround = Physics.Raycast(path.vectorPath[currentWaypoint], Vector3.down, 1.5f, terrainLayers);
            }

            if (ai.verboseDebug)
            {
                DebugDrawPhysics.DebugDrawCircle(path.vectorPath[currentWaypoint], 0.25f, isOnGround ? Color.green : Color.red, 1f / ai.thinkPerSecond);
            }

            return(isOnGround);
        }
Beispiel #3
0
        /// <summary>
        /// Follows a path by running and jumping
        ///
        /// returns true if reached end of path
        /// </summary>
        bool FollowPath()
        {
            if (!pathfinding.IsOnPath())
            {
                pathfinding.SetPathUnvalid();
                if (verboseDebug)
                {
                    Debug.Log(name + ": " + state + ": I was no longer on my path so requested a new one");
                }
                return(false);
            }
            //Find next waypoint that is on the ground. If the last one is in the air then we should jump to it
            pathfinding.FindNextVisibleGroundedWaypoint(terrainLayers);

            //Direction to the next waypoint
            Vector3 dist = (pathfinding.GetCurrentWaypoint() - position);
            Vector3 dir  = dist.normalized;

            //If verbose is on then we can draw some helper symbols to see what the character is currently trying to do
            if (verboseDebug)
            {
                DebugDrawPhysics.DebugDrawCircle(pathfinding.GetCurrentWaypoint(), 1, Color.yellow);
            }

            //If there's a hole in the ground and the waypoint is not below us then jump
            doJump = DetectHolesInGround() && dir.y >= 0;

            //---Run---
            if (dir.x > 0)
            {
                run = 1;
                //Jump if we need to move up, double jumps will occur when we reach the max of the parabola and thus the upwards velocity is zero
                if (dist.y > 1 &&    //The next waypoint is at least one meter up
                    Vector3.Angle(Vector3.right, dir) > angleToJump &&
                    velocity.y <= 0) //A velocity zero we reached the top of the jump parabola
                {
                    doJump = true;
                }
            }
            else
            {
                run = -1;
                //Jump if we need to move up
                if (dist.y > 1 &&
                    Vector3.Angle(Vector3.left, dir) > angleToJump &&
                    velocity.y <= 0)
                {
                    doJump = true;
                }
            }

            //If we have a low obstacle then we should try jumping
            //Note that we will jump regardless of whether we detect an upper obstacle or not
            //We will only jump when we do not have upwards velocity so that we do not waste any double jumps
            if (isDetectingLowObstacle && velocity.y <= 0)
            {
                doJump = true;
            }
            //Check if we are close enough to the next waypoint
            //If we are, proceed to follow the next waypoint
            return(pathfinding.SelectNextWaypointIfCloseEnough());
        }