- Home /

# What's the easiest way to see if a sphere intersects with another using insideUnitSphere

Hello,

I am trying to create multiple clusters of objects in a sphere radius,

however these clusters should not intersect with one another.

Here is a visual of how it should be:

So I have this to get my radius:

```
public Vector3 GetCircleRadius(Vector3 center, float maxDistance)
{
return (Random.insideUnitSphere + center) * maxDistance;
}
```

Say I would call that method twice on a different center & max distance.

What would be the easiest formula I can use to check if these 2 Vector3's overlap eachother?

Thanks for any suggestions!

Kr, Shady

**Answer** by Bunny83
·
Feb 19 at 11:38 AM

From your code and your image it's not clear what radius your "clusters" have. Your method is called "GetCircleRadius" but it doesn't seem to return any radius. Actually the method doesn't make much sense to me. You add a random vector to the center and then you scale the result by maxDistance. This doesn't make much sense. Maybe you wanted to do something like this:

```
public Vector3 GetPointInSphere(Vector3 center, float sphereRadius)
{
return center + Random.insideUnitSphere * sphereRadius;
}
```

It's still not clear what you want to check. This method will produce positions which are inside the sphere defined by the center and the sphereRadius. A single position doesn't have any size and therefore can't overlap with another position. Maybe your positions have some sort of size?

Or was the question how to ensure that your coosen center / radius does not overlap with another center / radius? In this case you should first clear up if this is a 2d or a 3d issue. So do we talk about circles or spheres? Currently your code does represent a sphere (3d).

If this is about 2d circles (like your drawing) you shouldn't use "Random.insideUnitSphere". If you flatten / project the vector you get from insideUnitSphere down to 2d the distribution would be biased. You get a higher density near the center than the perimeter. You should use Random.insideUnitCircle for 2d.

To test if two circles or spheres overlap you just have to calculate the distance between the center points of those circles / spheres and compare it to the sum of their radii. If the distance between the centers is smaller than the sum, they overlap, otherwise they don't.

It's still not fully clear what you want to do. You should be more clear about what you actually want to do. Also if this is 2d or 3d.

The project is in 2D, so insideUnitCircle would indeed be the better option for getting a random point in the circle.

I want to create 4 circles at random positions that don't intersect eachother, And then create a number of game objects inside these circles at random positions.

So I assume this would be the correct way to do the checks and getting a random position inside one of the circles:

```
public Vector2 GetRandomPointInCircle(Vector2 center, float radius)
{
return center + Random.insideUnitCircle * radius;
}
public bool IntersectsWith(Vector2 center1, Vector2 center2, float radius1, float radius2)
{
return Vector2.Distance(center1, center2) >= (radius1 + radius2);
}
```

So to clear things up, the actual question was indeed so see if two circles don't overlap with eachother.

Right, you just got the comparison the wrong way. Your IntersectsWith method would return true when they don't intersect and would return false when they do intersect ^^. It should be:

```
return Vector2.Distance(center1, center2) < (radius1 + radius2);
```

This will return true when they intersect and returns false when they don't.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

How do I find slope on a spherical terrain 2 Answers

Mesh creation by code not working? 0 Answers

Project a vector onto a plane. 2 Answers

What is wrong with this rigidbody? 2 Answers