- Home /
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?
Answer by dorpeleg · Aug 27, 2013 at 07:33 AM
Just make the raycast from you HUD camera instead of the main camera.
Example:
public Camera HUDcamera;
Ray ray = HUDcamera.ScreenPointToRay(Input.mousePosition);
Answer by graslany · Aug 27, 2013 at 08:05 AM
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 ?
Your answer
Follow this Question
Related Questions
How to move the camera closer to the player while there is an object between them? 1 Answer
Camera rotate to look at GameObject from Raycast 3 Answers
Unity2D - Camera in exported game is zoomed out compared to unity game preview HELP PLEASE! 2 Answers
flickering lines when zooming fov (both GUI and ortho cameras). 2 Answers
Moving camera around with GUI buttons 4 Answers