- Home /
Question by
LijuDeveloper · Dec 02, 2013 at 09:31 AM ·
cameramousepositionviewcamerasmouseover
Mouse position in camera view area
How to find a camera in purticular camera's view position ? For example
In this example , mouse position in " Camera 2 " view . How to find mouse position in camera view( Not using Raycast or OnMouseOver() function )
campos.png
(11.8 kB)
Comment
The mouse position in what coordinates? World? Viewport?
Best Answer
Answer by Tomer-Barkan · Dec 02, 2013 at 10:01 AM
Use Camera.ScreenToViewportPoint to get the mouse's position relative to the camera's viewport.
If both X and Y are between 0 and 1, then it is within the camera's viewport, otherwise it is outside:
public Camera GetCameraForMousePosition() {
Camera[] allCameras = Object.FindObjectsOfType(typeof(Camera)) as Camera[];
foreach (Camera camera in allCameras) {
Vector3 point = camera.ScreenToViewportPoint(Input.mousePosition);
if (point.x >= 0 && point.x <= 1 && point.y >= 0 && point.y <= 1) {
return camera;
}
}
return null;
}
By the way, ins$$anonymous$$d of :
Camera[] allCameras = Object.FindObjectsOfType(typeof(Camera)) as Camera[];
you can simply use the following :
Camera[] allCameras = Object.FindObjectsOfType<Camera>();
This is the suggested practice.