- Home /

# Make sphere collider the center of gameobjects/Sum of vectors

Hi, I have problems setting the center of a sphere collider at the center of spawned gameobjects, it moves but not to the center of the gameobjects.

edit: I've found that the sum of Vectors doesn't give the correct number, thats the problem, but i don't know why it happens

This is the code:

```
void SphereRadious()
{
//4/3 * pi * r^3 = V ; Vt = V * (protons + neutrons) ; r^3 = Vt/(4/3 * pi) ; r = sqrt(Vt/(4/3 * pi))
float atomVol = 4 / 3 * Mathf.PI * 1;
float totalVol = atomVol * (protonsAmount + neutronsAmount);
float r = Mathf.Pow(totalVol / ((4 / 3) * Mathf.PI), 1f/3f);
sphereCollider.radius = r;
radiousOfSpawn = r;
//PROBLEM:
float x = 0;
float y = 0;
float z = 0;
for (int p = 0; p < protonsAmount; p++)
{
for(int n = 0; n < neutronsAmount; n++)
{
x = 0; y = 0; z = 0;
x += (protons[p].transform.localPosition.x + neutrons[n].transform.localPosition.x);
y += (protons[p].transform.localPosition.y + neutrons[n].transform.localPosition.y);
z += (protons[p].transform.localPosition.z + neutrons[n].transform.localPosition.z);
sphereCollider.center = new Vector3(x, y, z);
}
}
}
```

edit2: i've also tried this but the same result:

```
float x = 0;
float y = 0;
float z = 0;
for (int p = 0; p < protonsAmount; p++)
{
float xp = protons[p].transform.localPosition.x;
float yp = protons[p].transform.localPosition.y;
float zp = protons[p].transform.localPosition.z;
x += xp;
y += yp;
z += zp;
}
for (int n = 0; n < neutronsAmount; n++)
{
float xn = neutrons[n].transform.localPosition.x;
float yn = neutrons[n].transform.localPosition.y;
float zn = neutrons[n].transform.localPosition.z;
x += xn;
y += yn;
z += zn;
}
sphereCollider.center = new Vector3(x, y, z);
```

Can you try adding a child game object of a small Cube to your game object, with local position of 0,0,0? Then post a picture of your game object selected?

yes, the spawned game object that you want to center the collider on

**Answer** by bsmx125
·
Jun 06 at 12:33 PM

```
float x = 0;
float y = 0;
float z = 0;
for (int p = 0; p < protonsAmount; p++)
{
//x = 0; y = 0; z = 0;
float xp = protons[p].transform.localPosition.x;
float yp = protons[p].transform.localPosition.y;
float zp = protons[p].transform.localPosition.z;
x += xp; y += yp; z += zp;
}
for (int n = 0; n < neutronsAmount; n++)
{
float xn = neutrons[n].transform.localPosition.x;
float yn = neutrons[n].transform.localPosition.y;
float zn = neutrons[n].transform.localPosition.z;
x += xn; y += yn; z += zn;
}
float totalPar = neutronsAmount + protonsAmount;
x /= totalPar; y /= totalPar; z /= totalPar;
sphereCollider.center = new Vector3(x, y, z);
```

**Answer** by Sazails
·
Jun 05 at 04:31 PM

Hopefully this is what you mean?

```
public GameObject[] GOs;
private float totalX = 0f;
private float totalY = 0f;
private float totalZ = 0f;
public Vector3 center; // This is the center of all the objects
void Start()
{
foreach (GameObject GO in GOs)
{
totalX += GO.transform.position.x;
totalY += GO.transform.position.y;
totalZ += GO.transform.position.z;
}
center.x = totalX / GOs.Length - 1;
center.y = totalY / GOs.Length - 1;
center.z = totalZ / GOs.Length - 1;
}
void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(center, 0.5f);
}
```

**Answer** by highpockets
·
Jun 05 at 04:38 PM

So the sphere collider is on the parent of all the neutrons and protons, correct?? In the first script, you are setting x y and z to 0 on every iteration. Try this:

```
void SphereRadious()
{
//4/3 * pi * r^3 = V ; Vt = V * (protons + neutrons) ; r^3 = Vt/(4/3 * pi) ; r = sqrt(Vt/(4/3 * pi))
float atomVol = 4 / 3 * Mathf.PI * 1;
float totalVol = atomVol * (protonsAmount + neutronsAmount);
float r = Mathf.Pow(totalVol / ((4 / 3) * Mathf.PI), 1f/3f);
sphereCollider.radius = r;
radiousOfSpawn = r;
//PROBLEM:
float x = 0;
float y = 0;
float z = 0;
for (int p = 0; p < protons.Length; p++)
{
for(int n = 0; n < neutrons.Length; n++)
{
x += (protons[p].transform.localPosition.x + neutrons[n].transform.localPosition.x);
y += (protons[p].transform.localPosition.y + neutrons[n].transform.localPosition.y);
z += (protons[p].transform.localPosition.z + neutrons[n].transform.localPosition.z);
}
}
float totalParticles = protonsAmount + neutronsAmount;
x = x / totalParticles;
y = y / totalParticles;
z = z / totalParticles;
sphereCollider.center = new Vector3(x, y, z);
}
```

One thing that I can't wrap my head around is how you are not getting an error comparing an int to a float in your loop. I imagine protonsAmount/neutronsAmount are floats, but you compare them to ints in your for loop without casting. I changed that to protons.Length and neutrons.Length.

Nah, the protonsAmount/neutronsAmount are ints xd

With that code it happens the same that with other code i've tried, although with < 4 it works fine (with your code and other i've done) when i spawn more it gets weird.

### 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

Problem with colliders when they get on each other. 1 Answer

What is the best way to check is object intersecting? 1 Answer

Reset a collider during runtime to lose previous Physics.IgnoreCollision() calls 1 Answer

Why is it so fast to move objects with collider collisions? 1 Answer

How to check is object directly next to another object? 1 Answer