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 )
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 )
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, instead 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.
– kamran-bigdely
The mouse position in what coordinates? World? Viewport? [Camera.ScreenToWorldPoint()][1]; [Camera.ScreenToViewportPoint(][2]); [1]: http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToWorldPoint.html [2]: http://docs.unity3d.com/Documentation/ScriptReference/Camera.ScreenToViewportPoint.html
– robertbu