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);