How to detect if a target is directly under an object and clamp the X look rotation

9533143--1345591--upload_2023-12-16_8-26-54.jpeg

I would like to know if these two things are possible to do preferably with one line of code.

I have a current angle check which returns the total angle to the target. The problem is it’s account for total direction but I want to know if the target is under it only.

    public float IsFacingTarget(this Transform source, Transform target, bool ignoreY = true)
    {
        Vector3 dir = target.position - source.position;
        Vector3 forward = source.forward;
        if (ignoreY)
        {
            dir.y = 0f;
            forward.y = 0f;
        }
        float angle = Vector3.Angle(dir, forward);
        return angle;
    }

The second thing I would like to know is how I would be able to clamp the final rotation X to not extend past certain points like in this image. The target is directly underneath the enemy but its X does not extend past 50.

I think what I’m actually looking for is to limit the direction of the laser itself but again I’m not sure what’s possible. I do not want the laser to be able to go past a certain range i.e. an enemy shooting a laser cannot hit it’s on feet.

   transform.LookAt(target.position);
   transform.eulerAngles=new Vector3(Mathf.Clamp(-Mathf.DeltaAngle(transform.eulerAngles.x,0),-80,50),transform.eulerAngles.y,0); // clamp the x axis

Going to try this, thanks!

It works but is there a way to convert this to Quaternion Lerp? I added the second line to what I have at the moment but it doesn’t work.

        Vector3 lookVector = target.position - transform.position;
        Vector3 rotationDirection = Vector3.RotateTowards(transform.forward, lookVector, lerpTime * Time.deltaTime, 0.0f);
        Quaternion newRot = Quaternion.LookRotation(rotationDirection);
        transform.rotation = newRot;
        transform.eulerAngles = new Vector3(Mathf.Clamp(-Mathf.DeltaAngle(transform.eulerAngles.x, 0), -_maxY, _maxY), transform.eulerAngles.y, 0); // clamp the x axis
transform.rotation=Quaternion.Lerp(transform.rotation,Quaternion.LookRotation(target.position-transform.position),50*Time.deltaTime);
transform.eulerAngles=new Vector3(Mathf.Clamp(-Mathf.DeltaAngle(transform.eulerAngles.x,0),-80,50),transform.eulerAngles.y,0); // clamp the x axis

Looks promising, thank you!
EDIT: It works great but is there a way to get the Vector3 position of the clamped position. I am firing a laser in that direction rather than using the transforms forward rotation.