Raycasting Stops working once I look more than halfway up the screen with the camera - #Devtober

Hello there, I am a returning unity developer and after hours of working and checking for errors, I have come across a problem that I cannot seem to solve. Once I look up with my fps controller the raycast stops working until i look back down at about halway down. It is not seeming to be a layer issue, attaching and running the script to a simple cam and rotating it has the same result. Tried using Screen height / width divided by 2, no help. What’s going on?? Did anyone run into this issue? I really need to get this script to work, and no forum solution worked for me so far.

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

public class Gun : MonoBehaviour
{
  public Camera Source;
    public LayerMask IgnoreMe;

    private void FixedUpdate()
    {
        RaycastHit hit;
        Ray ray = Source.ScreenPointToRay(Input.mousePosition);

        if (Input.GetKey(KeyCode.X))
        {
            if (Physics.Raycast(ray, out hit, Mathf.Infinity))
            {
               
                Debug.DrawLine(ray.origin, ray.direction * 1000, Color.red);
                print(hit.transform.gameObject.name);

            }
        }
    }
}

Can you be more specific about what you mean when you say that it “stops working”? What are you expecting to happen and what actually is happening?

Sorry, I was up all night trying to figure out what’s wrong. And, as always, my problem is passing plain logic and simple stuff without noticing. The code’s order dictates that the debug ray will be shown if there is something hit, however as my walls were exactly half of the player’s length, it seemed to be an engine related problem. Checked everything from addons to controllers, from layer masks to physics, until I figured i would place a wall high up to check for the ray, and guess what… it works as intended. So if anyone comes across this problem , don’t forget that the raycast code will be shown only once there is something hit.

Here is a revised code that works for anyone needing the raycast script, no layer masks.

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

public class Gun : MonoBehaviour
{
    public Camera Source;
    public LayerMask IgnoreMe;

    private void Update()
    {
        RaycastHit hit;


        if (Input.GetKey(KeyCode.X))
        {
            if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, Mathf.Infinity))
            {
                {
                    print(hit.transform.gameObject.name);
                    Debug.DrawLine(Camera.main.transform.position, Camera.main.transform.forward * 10000, Color.red);



                }
            }
        }
    }
}