But when I give a position of Cube as you see below. It's not drawing a line from camera through position of cube.
That's a position in the world. That means it gives the number of meters east, up, and north, measured from the world origin point.
The function name tells you it works with positions on the screen. That means it expects a number of pixels right and up, measured from the bottom-left corner of the game window.
If you want to convert a point in the world to a point on the screen, you can use Camera.WorldToScreenPoint.
When I give Input.mousePosition as parameter it works correctly.
Input.mousePosition is a position on the screen. So it's already in the correct coordinate system for Camera.ScreenPointToRay to work with.
How can Unity draw a line when my mouse is out of the boundary of game screen?
Because the projection math it's using does not care where the border of the window is. If the cursor is some number of pixels right and some number of pixels up from the center of the window, that corresponds to a ray with some particular yaw and pitch angle relative to the camera's facing direction. If those angles happen to be outside the camera's field of view, so be it. Every point on the infinite plane formed by extending your screen without limit in all directions still maps to a valid direction in the world, whether or not you can see that direction it in the rendered output.
In code it would be implemented something a bit like this (assuming a symmetric view frustum):
Ray ScreenToWorldPoint(Vector3 screenPos) {
// Remap so (0, 0) is the center of the window,
// and the edges are at -0.5 and +0.5.
Vector2 relative = new Vector2(
screenPos.x / Screen.width - 0.5f,
screenPos.y / Screen.height - 0.5f
);
if (!orthographic) {
// Angle in radians from the view axis
// to the top plane of the view pyramid.
float verticalAngle = 0.5f * Mathf.Deg2Rad * fieldOfView;
// World space height of the view pyramid
// measured at 1 m depth from the camera.
float worldHeight = 2f * Mathf.Tan(verticalAngle);
// Convert relative position to world units.
Vector3 worldUnits = relative * worldHeight;
worldUnits.x *= aspect;
worldUnits.z = 1;
// Rotate to match camera orientation.
Vector3 direction = transform.rotation * worldUnits;
// Output a ray from camera position, along this direction.
return new Ray(transform.position, direction);
} else {
// Scale using half-height of camera.
Vector3 worldUnits = relative * orthographicSize * 2f;
worldUnits.x *= aspect;
// Orient and position to match camera transform.
Vector3 origin = transform.rotation * worldUnits;
origin += transform.position;
// Output a ray from this point, along camera's axis.
return new Ray(origin, transform.forward);
}
}