Raycast misses after camera zoom

Hi there, I have two cameras in my game scene, the main camera and the HUD camera, both orthographic. The HUD camera shows the scores and pause button and the main camera shows the other game objects.

When the game is about to finish, the scene will be zoomed out by increasing the “size” of the main camera. That’s when the problem happens. After the game is done, a menu with “restart” and “home” button will pop up in the HUD camera.

if(Application.platform==RuntimePlatform.OSXEditor){
	if(Input.GetMouseButtonDown(0)){
		Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		RaycastHit hit;

        if(Physics.Raycast (ray,out hit)){
			if(hit.collider.tag == "Restart"){
                Application.LoadLevel("PlayScene");
            }
			else if(hit.collider.tag == "Home"){
                Application.LoadLevel("HomeScene");
            }
        }
    }
}

The buttons cannot be pressed when the main camera is zoomed out. Is it because of the position of the buttons has changed according to the main camera? Can anyone suggest how I can solve this problem?

Hello,

You write that the menu items are displayed on the HUD camera, and you are casting a ray from the main camera. Could this be the issue ?

Just make the raycast from you HUD camera instead of the main camera.

Example:

public Camera HUDcamera;

Ray ray = HUDcamera.ScreenPointToRay(Input.mousePosition);