Responding to mouse clicks over 2 different camera areas

Hello all,

If you take a look at the picture I included, you can see I’m making a fairly complex GUI which includes two cameras. I would like to be able to orbit each camera separately if the user clicks and drags while his mouse is over a camera image.

My question is this: is there a simple way to know if a user is clicking over part of the screen occupied by camera 1 or camera 2??

Thanks!

Check out Rect.Contains(Vector2 point) and send in mouseposition as the point.

That’s a good tip, Slem. Ideally I’d make it work with something like:

var myRect = camera.rect;
// or:
var myRect = Camera.Main.rect;
// then:
if( myRect.rect.Contains(Input.mousePosition))
{
// Do my stuff...
}

but that’s not working. Woulda been slick, though, yeah?

:wink:

I’m also having difficulties putting my Background image behind the camera areas. Any ideas about that while we’re here?

I know this is way late, but I came over this again and let me clear this up for others:
Camera.rect refers to the camera’s Normalized Viewport Rect which being normalized ranges from 0 to 1.
Input.mousePosition gives a Vector2 in screenSpace i.e 832, 201, ergo the Rect.Contains will never yield true.

To properly check if the input is within the bounds of a camera’s rect you need to create a Rect which represent the normalized Rect in Screen space.

This really just means multiplying left and width with Screen.width and top and height with Screen.height, except that the normalized (0,0) is bottom left corner and not top left as with Screen space. This can easily be fixed by “inverting” the top before multiplying with Screen.height.

Rect cameraScreenSpace = new Rect(Camera.main.rect.left * Screen.width, (1-Camera.main.rect.top) * Screen.height, Camera.main.rect.width * Screen.width, Camera.main.rect.height * Screen.height);

You should probably make a method that does this more cleanly. And if you’re using C# you can create Extension methods. Like so (must be placed in a static class)

public static Rect ToScreenSpace(this Rect rect)
{
return new Rect(rect.left * Screen.width, (1-rect.top) * Screen.height, rect.width * Screen.width, rect.height * Screen.height);
}

You can then use the Contains method with Input.mousePosition as the parameter.

if(Camera.main.rect.ToScreenSpace().Contains(Input,mousePosition)
{
//Do stuff
}

EDIT: I havent tested this, but I just remembered that top and left are obsulete and you should use yMin and xMin instead.