I haven’t made any changes to the projectile logic but this last time i opened my project to test no collision get detected. Each game object has a collider and a ridgidbody.
Here is my code:
void OnCollisionEnter(Collision collision)
{
var rigidbodies = new List();
// for every object within the explosion radius with a rigidbody component, add them to the rigidbody list
Collider[] objectsInRange = Physics.OverlapSphere(transform.position, ExplosionRadius);
foreach (Collider hit in objectsInRange)
{
if (hit.attachedRigidbody != null && !rigidbodies.Contains(hit.attachedRigidbody))
{
rigidbodies.Add(hit.attachedRigidbody);
}
// if any object has a target script attached, run the take damage function, with a fall-off effect, i.e. the further away the target is from the explosion origin, the less damage they take
Target TargetHit = hit.GetComponent<Target>();
if (TargetHit != null)
{
float proximity = (transform.position - TargetHit.transform.position).magnitude;
float effect = ExplosionDamageFalloff - (proximity / ExplosionRadius);
// if the target is very close to the explosion origin, take full damage
if (proximity <= 0.7f)
{
TargetHit.TakeDamage(ExplosionDamage);
}
// else, take splash damage
else
{
TargetHit.TakeDamage(ExplosionDamage * effect);
}
//StartCoroutine(playAudio(ExplosionSound));
Destroy(gameObject);
}
else
{
Rigidbody rb = hit.GetComponent<Rigidbody>();
rb.AddExplosionForce(ExplosionForce, transform.position, ExplosionRadius, ExplosionUpwardsForce);
//StartCoroutine(playAudio(ExplosionSound));
Destroy(gameObject);
}
}
}