- Home /
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! :)
Could you just get the x position of the camera and if less than half the screen, raycast from left camera, else use right viewport camera? Or did I miss something?
Hmmmm, maybe - however it feels like there should be some more sophisticated way of doing it... $$anonymous$$aybe I could use a load of if-statements to cut out the positions the viewports and comparing them to the position of the mouse, but how would anyone go about to achieve the same result with, say - an elipse or circular $$anonymous$$imap? :o
Answer by The_Toller · Jun 13, 2015 at 01:39 PM
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
}
else
{
//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. :-)
Answer by FortisVenaliter · Jun 11, 2015 at 09:05 PM
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.
Is there a read-variable for viewport bounds? How do you mean I should set up a mouse click controller?
I mean you should have a single controller monobehaviour class that checks Input.Is$$anonymous$$ouseDown.
That class should store a reference to each $$anonymous$$imap. When it detects a mouse press, it checks with the $$anonymous$$imap instances to see if the mouse position is in their bounds, and only if it isn't does it check against the main camera.
Your answer
Follow this Question
Related Questions
Isometric Camera : Get the displayed terrain portion. 1 Answer
WorldToViewportPoint and WorldToScreenPoint give wrong positions when VR is enabled 1 Answer
Move the camera in 2D when target reaches bounds. Is there a better way? 0 Answers
Convert UI Image size and position to Camera viewport rec. 1 Answer