How to get Mouse position in World space in Unity


How to get Mouse position in World space in Unity:
- In Unity we have various properties and methods, which help us to get mouse position in screen space, viewport space, world space etc.

How to get Mouse Position in world space:
- world space is the Unity scene space or scene coordinate system
- is measured in meters

W.K.T. Input.mousePosition returns mouse position in screen space in pixels

Camera.ScreenToWorldPoint() method:
public Vector3 ScreenToWorldPoint(Vector3 position)
- Converts and returns a given position in screen space to position in world space relative to the current camera transform
position.x = Input.mousePosition.x;
position.y = Input.mousePosition.y;
position.z = indicates at what distance from the camera's z position you want to get the mouse position

Example Code:

if (Input.GetMouseButtonUp (0))
Debug.Log (Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x,Input.mousePosition.y,0f)));

float height = 2f * cam.orthographicSize; // 2f * 5f = 10f
float width = height * cam.aspect; // 10f * (600/400) = 10f * 1.5f = 15