Raycast to Stop a GUI

Title is kinda crap but what I mean is that I want to sense how far I am away from a certain object. I’ve used a raycast to do this but I have to be facing the abject so as it detects how far away I am from it. Is there any way I can make the raycast follow or stay in the same direction as the object or something? Thanks!
Edit: Also, when I move my screen off the object, then the raycast doesn’t touch it so it acts as if am far away from it but I’m not.

Raycasts can go in any direction, not just forward. Check the user reference for Raycasting to see how to influence the direction of a ray.

Also, with Vector3.Distance you don’t even have to use raycasts.