I would define your Ray like this:
Ray ray = Camera.Main.ScreenPointToRay(Input.mousePosition);
As for your code, I can't test your code right now so I don't know what exactly goes wrong. Because you should be able to do as you described, even though my answer is an easier alternative.
You can use Debug.DrawLine to draw a line of the raycast you use. Then, you can see what your raycast looks like in the debugger, during runtime. You can see then how your raycast is positioned. You'd be able to see the start and end point of the ray. If one of them is off, you can check your code while knowing which part is wrong.