Detecting camera viewport on mouse down?


I'm trying to do a chat interface with a camera viewport that takes up only a part of the entire screen. The rest of the screen space is used for chat boxes and other interface stuff.

The user should be able to rotate the camera around the playing area, which is displayed in the camera viewport, somewhat akin to Homeworld or WoW style controls. When the user presses the right mouse button, I want to find out if the mouse pointer is inside the viewport or not. If it is not, nothing should happen (not even if the mouse is moved into the viewport after clicking). If the mouse pointer is over the viewport and the right mouse button is clicked and held down, the camera should rotate around an arbitrary focus point (conveniently named the "character" in my project), based on mouse movement.

I'm figuring out how to do camera rotation and such by looking at other scripts, so answers need not include that (although a really simple script suggestion would of course be nice :) ), but I have no idea how to check whether the mouse pointer is inside the viewport.

Ideally, buttons occluding the camera viewport should also prevent the camera controls from becoming active if the mouse pointer is pointing at them.

I would like to post some coding examples, but what I have is not my programming (I don't fully grasp it and it is miles long), and as for the mouse check I haven't been able to write a single line yet - I simply don't know how to get started. :(

Thanks in advance!

You can get a cameras normalized viewport with camera.pixelRect This will give you the area of the screen that this camera renders to. All you need to do is find out if Input.mousePosition is inside that rectangle. You can use this:

if (Input.GetButtonDown("Fire1") && Camera.main.pixelRect.Contains(Input.mousePosition))

Not tested, but this should give you an idea.