I have an even distribution of points on the surface of a sphere and a list of Vectors for raycasting from the center, however when I try doing all of the casts the directions seem to be in a 270 degree cone in the direction the original (now stationary) object was moving that I am performing the casts from.
Does anyone know why this warp is happening and how to avoid it?
Vector3 explosionPosition = transform.position;
List<Vector3> points = new List<Vector3>(BlockIndex.SphereSurfacePoints(radius + 2));
float rayDamage = damage * 0.0027777777777778f;
float thisRay = rayDamage;
RaycastHit[] hits;
float fade = 1 / radius * rayDamage;
float forcefade = 1 / radius * explosionForce;
SortHits sorter = new SortHits();
rigidbody.isKinematic = true;
collider.enabled = false;
int i = 0;
for (int x = 0; x < points.Count; x++)
{
// reset vars befor raycast
thisRay = rayDamage;
i = 0;
//cast a ray for the current point
hits = Physics.RaycastAll(explosionPosition, points[x], radius);
if (hits != null && hits.Length > 0)
{
foreach (RaycastHit hit in hits)
{
if (thisRay > 0)
{
DestructableObject dObject = hit.collider.GetComponent<DestructableObject>();
if (dObject != null)
{
float currdamage = dObject._currentHP;
dObject.ApplyRayDamage(thisRay);
thisRay -= currdamage;
}
if (hit.collider.rigidbody)
hit.collider.rigidbody.AddExplosionForce(
forcefade,
explosionPosition,
radius,
0);
}
if (thisRay > 0)
thisRay -= fade;
else
i = hits.Length;
}
}
}
return;
SphereSurfacePoints is the precomputed list of Vector3 coordinates. To debug I tried instantiating cube primitives at each of the points and it makes a perfect sphere.
This is the function that returns the array:
public static Vector3[] UniformPointsOnSphere(float numberOfPoints)
{
List<Vector3> points = new List<Vector3>();
float i = Mathf.PI * (3 - Mathf.Sqrt(5));
float offset = 2 / numberOfPoints;
float halfOffset = 0.5f * offset;
float y = 0;
float r = 0;
float phi = 0;
int currPoint = 0;
for (; currPoint < numberOfPoints; currPoint++)
{
y = currPoint * offset - 1 + halfOffset;
r = Mathf.Sqrt(1 - y * y);
phi = currPoint * i;
Vector3 point = new Vector3(Mathf.Cos(phi) * r, y, Mathf.Sin(phi) * r);
if (!points.Contains(point)) points.Add(point);
}
return points.ToArray();
}