- Home /
Projectile collision not detecting.
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);
}
}
}
Answer by unity_ek98vnTRplGj8Q · Feb 05, 2020 at 01:33 AM
First make sure that it actually is the collision that's the issue. I recommend putting a debug statement at the beginning of the function to see if it gets called at all.
If you don't see any collision then make sure your objects are on the correct layers, and that these layers are set in the collision matrix in your projects physics settings. Make sure your colliders do not have the trigger checkbox checked. I think if all these things are correct you should be seeing collisions.
If you still aren't try debugging with a super slow projectile. Often projectiles move too fast to trigger collisions (depending on what you are doing) and raycasting may be a better solution for you