Unfortunately, rotating the ray based off the event.relative does not quite work all around the screen, because the camera/screen pixels are not evenly spaced angularly like on a protractor. Instead, they are evenly spaced in a flat plane in front of the camera.
But you don't really need to know the math behind it, because Godot has a function which does it for you:
The camera node comes with the function
to get the vector which the mouse is pointing in.
To orient the RayCast to the correct direction, you could use
look_at(position,up_direction) BUT this function only points the -Z side of the object towards the position, so make sure your ray's
cast_to property is in the direction
So for example:
onready var ray = $RayCast
if e is InputEventMouseMotion: