Check If Target Within Rotation Range

Hello, i’m trying to check if a target enters the turret’s range the turret doesn’t rotate towards the target unless the target is within the rotation range of the turret.
the turret have a max and min rotation angles that it cant rotate beyond them.
currently the turret rotates towards target and doesnt rotate beyond max and min angles

public class MountedDefenseTargetEnemyController : MonoBehaviour
[SerializeField] private float minAngle;
[SerializeField] private float maxAngle;
[SerializeField] private float rotationSpeed;
[SerializeField] private Transform rotator;
private bool isTargetSet;
private Quaternion targetRotation;
private GameObject target;
private Vector3 directionToTarget;

public void CheckTargetWithinAngleThreshold(GameObject target)
    // if(IsTargetWithinRotationAngleRange())
    // { = target;
        isTargetSet = true;
    // }

void Update() 

    directionToTarget = target.transform.position - rotator.position;

    // Get the angle between the turret's rotation and the direction to the target
    float angleToTarget = Vector3.Angle(rotator.forward, directionToTarget.normalized);
    // Check if the target is within the angle range.
    bool isTargetInRange = angleToTarget >= maxAngle && angleToTarget <= minAngle;

    // Check if the target is within the turret rotation range
    if (isTargetInRange ) 
        directionToTarget = target.transform.position - rotator.position;
        directionToTarget.y = 0;
        targetRotation = Quaternion.LookRotation(directionToTarget, Vector3.up);
        targetRotation.eulerAngles = new Vector3(0, targetRotation.eulerAngles.y, 0);
        rotator.rotation = Quaternion.RotateTowards(
            rotator.rotation, targetRotation, rotationSpeed * Time.deltaTime);
        if (rotator.localEulerAngles.y >= minAngle && rotator.localEulerAngles.y < maxAngle - 3f)
            //Debug.Log("reached Max left angle");
            rotator.localEulerAngles = new Vector3(0f, minAngle, 0f);
        if (rotator.localEulerAngles.y <= maxAngle && rotator.localEulerAngles.y > minAngle)
            //Debug.Log("reached Max right angle");
            rotator.localEulerAngles = new Vector3(0f, maxAngle, 0f);


any guidance to what im doing wrong to check if the target within turret rotation range would be appreciated

In your CheckTargetWithinAngleThreshold method, you should check if the target is within the turret’s rotation range using the minAngle and maxAngle values. If it’s within the range, you set the target and allow rotation; otherwise, you should not set the target.