Raycasting depending on viewport

I am having some issues with raycasting from cameras using ScreenPointToRay.

In my game I have the original game view, rendered by the mainCamera and I have two ortographic cameras rendering from above serving as a world and local minimap (positioned top left and bottom left at about a fifth of the screen size each). Thing is, when I select a unit in my game I want to be able to use RMB for a context sensitive action. When I right-click a spot on any of my minimaps, my selected unit should interpret this as a move action. I have solved this by raycasting from the minimapcameras agains a ground collider. However, when I right-click a visible object in my original viewport, I need my unit to either move there, mine the target if it’s flagged as mineable or attack if flagged as enemy etc. This works great, the problem is that I don’t understand how I should tell my script which camera I want to raycast from based on viewport. At the moment, if I right-click the center of my screen, my main camera raycast is not returning anything, but my minimap-cameras still raycast and the unit starts to move to unknown locations. This also means that if I right-click in my minimapview and click an object, I get three instances of results. From the maincamera, worlmap camera and local camera.

I want it to only cast a ray from the maincamera when I’m not right-clicking inside the minimaps viewports, and from the worldcamera if i click the worldmap etc.

Thanks in advance! :slight_smile:

I would do it as a single-controller else if structure.

So, have a controller for mouse clicks. When it detects one, it checks first if the mouse position is within the screen bounds of the minimap. If it is, it uses that camera. If it’s not in any of the minimap areas on the screen, then you use the main camera. In this setup, the individual views would not check for a mouse click themselves, they would respond to the controller when it says they should handle a current mouse click.

At the moment I’ve “solved it” by using an interoperation of @FortisVenaliter 's suggestion.

	void CheckForObjectAndSelect()
		if(Input.mousePosition.x <= Screen.width / 8.5f && Input.mousePosition.y <= Screen.height / 7.6f)
			//User have clicked minimal
			//User have clicked main viewport

Although this solution is working for me at the moment, another solution would be necessary if the minimap was not rectangular. In that situation I guess the best way would maybe to attach colliders to the camera object that respond to raycasts, then again - that would need me to do two raycasts for each mouse click… One for the collidercheck, and one from the camera requested, ignoring the first colliders.

Let’s just hope that when the day comes when I need to set up a circular minimap, I have more scripting experience and I’m able to laugh at the stupid and clumsy way of coding I thought was the correct way. :slight_smile: