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
- Target Point Radius

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
// sphereRadius is the radius of the sphere
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);
// Solve quadratic formula
float D = D1 - D2 + D3;
// Check if the ray is within the radius
if (D < sphereRadius)
{
closestPointIndex = pointIdx;
}
}
```

I hope this helps. I don’t quite have a grasp on the math but I managed to figured this out by reading Sparkys answer in this thread algorithm - Testing whether a line segment intersects a sphere - Stack Overflow.

Cheers!