My enemy keeps shooting at me until I get close

Help, I am trying to make a code, where my enemy looks around for me, and then when he spots me, starts shooting slowly, but what ends up happening is from the start my enemy spam shoots me until I get 1 meter or closer to him, and when i back up he spam shoots me again! any help?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Enemy : MonoBehaviour
{
	public NavMeshAgent agent;
	public Transform player;
	public LayerMask whatIsGround, whatIsPlayer;
	public GameObject enemybulletPrefab; 
	public float health;

	//patrolling
	public Vector3 walkPoint;
	bool walkPointSet;
	public float walkPointRange;

	//attacking
	public float timeBetweenAttacks;
	bool alreadyAttacked;

	//states
	public float sightRange, attackRange;
	public bool PlayerInSightRange, PlayerInAttackRange;
 
	private void Awake()
	{
		player = GameObject.Find("Fps player").transform; 
		agent = GetComponent<NavMeshAgent>();
	}

	private void Patrolling() 
	{
		if (!walkPointSet)
		{
			SearchWalkPoint();
		}

		if (walkPointSet)
		{
			agent.SetDestination(walkPoint);
		}

		Vector3 distanceToWalkPoint = transform.position - walkPoint;

		//walkpoint reached
		if (distanceToWalkPoint.magnitude < 1f)
		{
			walkPointSet = false;
		}
	}

	private void SearchWalkPoint()
	{
		//calculate random point in range 
		float randomZ = Random.Range(-walkPointRange, walkPointRange);
		float randomX = Random.Range(-walkPointRange, walkPointRange);

		walkPoint = new Vector3(transform.position.x + randomX, transform.position.y, transform.position.z + randomZ);

		if (Physics.Raycast(walkPoint, -transform.up, 2f, whatIsGround))
		{
			;
		}

		walkPointSet = true;
	}

	private void ChasePlayer()
	{
		agent.SetDestination(player.position);
	}

	private void Attackplayer() 
	{
		//make sure enemy doesn't move
		agent.SetDestination(transform.position);
		transform.LookAt (player);
            
		//shooting
		GameObject bulletObject = Instantiate(enemybulletPrefab);
 
		bulletObject.transform.position = transform.position + transform.forward; 
 
		bulletObject.transform.forward = transform.forward;

		if (alreadyAttacked)
		{
			alreadyAttacked = true;
			Invoke(nameof(ResetAttack), timeBetweenAttacks);
		}
	}
	private void ResetAttack()
	{
		alreadyAttacked = false;
	}

	private void Update()
	{
		//checking if player is in sight or attack range
		PlayerInSightRange = Physics.CheckSphere(transform.position, sightRange, whatIsPlayer); 
		PlayerInAttackRange = Physics.CheckSphere(transform.position, attackRange, whatIsPlayer); 
            
		if (!PlayerInAttackRange && !PlayerInAttackRange)
		{
			Patrolling();
		}

		if (!PlayerInAttackRange && !PlayerInAttackRange)
		{
			ChasePlayer();
		}

		if (!PlayerInAttackRange && !PlayerInAttackRange)
		{
			Attackplayer();
		}
	}
	public void TakeDamage(int damage)
	{ }
}

So I’ve allowed myself to reformat your code and it revealed tons of problems and weird things right of the bat.

  1. In 62’nd line you had a ‘;’ right after the if statement, so walkPointSet was always set to true in this method, no matter what
  2. In Update each behavior is checking for !PlayerInAttackRange, so every behavior has the same condition which doesn’t look correct. Also you are checking the same condition twice.
  3. Your AttackPlayer method doesn’t even check for that AlreadyAttacked variable. It just shoots. The check at the end should be reversed (!AlreadyAttacked) and few lines higher, just before bullet instantiation at least
  4. Nitpicking, but don’t use GameObject.Find method. One day you’ll rename your object and will look for this bug for ages, it’s really not that easy to find
  5. Really, format your code. I’m not talking about formatting on this site (which you should also do) but about things like if statement and the method you call being in the same line. It’s 1000x more readable if they are in separate lines. I also really recommend braces for single line if statements, it helps notice things like what happened in 62nd line.