コード例 #1
0
ファイル: GenericEnemy.cs プロジェクト: waseem2k/CodeSnippets
    private bool FollowPath()     // If is on path it returns true. Also checks for collision and direction to move towards
    {
        //return true;
        if (!pathfinding.IsOnPath())
        {
            pathfinding.SetPathUnvalid();
            if (verboseDebug)
            {
                Debug.Log(name + ": " + enemyState + ": 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(groundLayer);

        //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 (showDetectors)
        {
            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
            //The next waypoint is at least one meter up
            if (dist.y > 1 && Vector2.Angle(Vector2.right, dir) > angleToJump && Velocity.y <= 0)             //A velocity zero we reached the top of the jump parabola
            {
                //Debug.Log("Jumping | Right");
                doJump = true;
            }
        }
        else
        {
            run = -1;
            //Jump if we need to move up
            if (dist.y > 1 && Vector2.Angle(Vector2.left, dir) > angleToJump && Velocity.y <= 0)
            {
                //Debug.Log("Jumping | Left");
                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 && !isDetectingHighObstacle)
        {
            //Debug.Log("Jumping | Detecting low angle");
            doJump = true;
        }

        //Check if we are close enough to the next waypoint
        //If we are, proceed to follow the next waypoint
        return(pathfinding.SelectNextWaypointIfCloseEnough());
    }