Check if raycast intersects a sphere given the spheres point and radius

I’m trying to do some custom editor scripting and I have handle sphere that indicate where a point is. What I want to do is when the mouse is hovering over a sphere, I want to change its colour to red. However I only have the following:

• Mouse Origin
• Mouse Ray/Direction
• Target Point

How do I figure out that the mouse ray is intersecting that sphere?

Cheers

never mind I figured it out:

``````// mouseRay is the ray that instantiates from the camera to the direction of the mouse
// Points is the list of all points i'm checking for

closestPointIndex = -1;
Vector3 mouseDir = mouseRay.direction.normalized;

for (int pointIdx = 0; pointIdx < Points.count; pointIdx++)
{
// Get the components of the vector coming from the point to the camera
float x = curveCreator.points[pointIdx].x - mouseRay.origin.x;
float y = curveCreator.points[pointIdx].y - mouseRay.origin.y;
float z = curveCreator.points[pointIdx].z - mouseRay.origin.z;

float t = (x * mouseDir.x + y * mouseDir.y + z * mouseDir.z) /
(mouseDir.x * mouseDir.x + mouseDir.y * mouseDir.y + mouseDir.z * mouseDir.z);

float D1 = (mouseDir.x * mouseDir.x + mouseDir.y * mouseDir.y + mouseDir.z * mouseDir.z) * (t * t);
float D2 = (x * mouseDir.x + y * mouseDir.y + z * mouseDir.z) * 2 * t;
float D3 = (x * x + y * y + z * z);