[Unity] Screen Mouse Position -> Screen Space (Camera) Position

I need to do this several time in my Unity game development journey. And I always forget how to do it, or forgot some small important detail, so I’ll write it here :).

First thing first, let’s have our mouse position on the screen.

Vector3 screenPos = Input.mousePosition;

I’ll call our Screen Space-Camera Canvas cameraCanvas. Here is small detail I always forget about.

screenPos.z = cameraCanvas.planeDistance;

With this we are ready to convert the position.

Camera renderCamera = cameraCanvas.worldCamera;
Vector3 canvasPos = renderCamera.ScreenToWorldPoint(screenPos);

And that’s all :)