How to stop the enemy from bugging when the player jumps and gets off from navmesh

Hi, i’m making an AI for my game, and while it reacts to sounds make by the player and by vision (can see the player), i’m having problem when the player is not on the ground.
While the player is walking/running, the IA works as intended, but when i jump (and i’m close to it), it starts shaking and bugging, the coordinates becomes a little buggy and the IA don’t know what to do, even tough it sees me.

if (angleToPlayer < 80 * Mathf.Deg2Rad)
        {
            // Player is in the cone, probably want to raycast to see if there's anything in the way   
            if (Physics.Raycast(ray, out hit2, canSeeDistance))
            {
                if (hit2.collider.tag != "Player")
                {
                    canSee = false;
                }
                else
                {
                    //transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(toPlayer), 3 * Time.deltaTime);
                    canSee = true;
                    lastKnownPosition = target.position;

                }
            }        
        }

and

 if(CanSeeTarget())
        {
            gunEnd.transform.LookAt(target);
            int distanceFromEnemy = 10;
            Vector3 TargetPos = (target.transform.position - transform.position).normalized * distanceFromEnemy;
            agent.SetDestination(TargetPos);
            patrolling = false;
            if(agent.remainingDistance < canShootDistance) 
            {
                //agent.stoppingDistance = Random.Range(20,41);
                if (weaponAmmo != 0 && Time.time > nextShot && canShot)
                {
                    nextShot = Time.time + fireRate;
                    //anim.SetTrigger("IsShooting");
                    ShootingSound();
                    actionOnPlayer();
                    weaponAmmo -= 1;
                }
                else
                {
                    StartCoroutine(Reload());
                }
            }
        }

I’m using a Navmesh and NavmeshAgent on the enemy, while on the player, i’m using a simple movement script (FPS one), so i can control it and go to places the navmesh agent can’t really go.

I believe the bug is in this part of the code:

lastKnownPosition = target.position;

and in the:

   Vector3 TargetPos = (target.transform.position - transform.position).normalized * distanceFromEnemy;
    agent.SetDestination(TargetPos);

in both of this parts, i just set a coordinate (the last position of the player) to be its next position instead of patrol the designated area. When the player is jumping/flying/up the navmesh, the code dictates it to just go to its next position (the player), but because the enemy can’t reach up on the place the player is, it starts bugging.

I don’t really know what to do now, i tried getting the player position on Y:0 (Ground level), but it bugs the AI.

Two solutions:.

First, do a check, if the player is in the air (or if a path isn’t find) just pause the agent. I think there’s a method for this but I don’t recall it exactly.

Second. Instead of navigating to the player themselves navigate to the x,z position on the mesh… You get what I mean? Get the player x,z position and use the y coordinate from the mesh. You can get this by raycasting down from the player to the mesh.