How to increase the range of the player aiming?

This is my whole script, I added it as a component to an anchor.
in the script, the anchor should follow the mouse position and rotate at its z-axis
BUT: The mouse moves 360 ° and I don’t want my anchor to move like that.
so The mouse should just follow the anchor when it is in front of the player

//__
public class aiming: MonoBehaviour
{
    public GameObject player;
    public GameObject anker;
    public void FixedUpdate()
    {
            Aim();
    }

    public void Aim()
    {
        Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

        float y = anker.transform.rotation.y;

        float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

        difference.Normalize();

        transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
    }
}
//___

So to simplify the question, you’d like to know if a particular point is in front of or behind a GameObject?


There are probably more efficient ways, but I like the Unity plane class.

You create a plane given the forward vector and position of your player. It has functions for determining size, distance and nearest point etc.

Plane P = new Plane(player.transform.Forward,player.transform.position);
if (P.GetSide(transform.position))
{
   //Yes, this is "In Front"
}

You can also compare the angle (Vector3.Angle) of your forward vector and the difference between the GameObjects positions if you’d like to narrow your field further - for example, the front 100 degrees rather than the full front 180 degrees.

float Angle = Vector3.Angle(player.transform.forward, (transform.position - player.transform.position).normalized);

This is even in degrees, so you don’t need to deal with radian conversion.

The solution I found is to make him just aim, when the mouse in inRange, to check that, I checked the value of the rotation in the z-axis. the value I wanted is 60 from both sides (60 and -60), that’s why I used the Mathf.Abs method.
here is my code now:

public class aiming : MonoBehaviour
{
    public bool inRange = false;
    public GameObject player;
    public void FixedUpdate()
    {
        Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
//-------------------SOLUTION--------------------------------------
        if (Mathf.Abs(rotationZ) >= 60)
        {
            inRange = true;
        }
        else
        {
            inRange = false;
        }

        if (!inRange)
        {
            Aim();
        }  
    }
//---------------------------------------------------------------------------
    public void Aim()
    {
        Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;

        float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;

        difference.Normalize();

        transform.rotation = Quaternion.Euler(0f, 0f, rotationZ);
    }
}