This code worked twice and then never functioned again. Why? It still plays the particle system and destroys the object, but doesn't damage or apply any force to objects with the Target script, a collider, and a rigidbody.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Explosive : MonoBehaviour
{
[SerializeField]
LayerMask damagableMask;
public int damage = 60;
public ParticleSystem explosionParticle;
public void Explode()
{
Instantiate(explosionParticle,gameObject.transform.position,gameObject.transform.rotation);
explosionParticle.Play();
Collider[] hitColliders = Physics.OverlapSphere(gameObject.transform.position,20f,damagableMask);
foreach (var hitCollider in hitColliders)
{
Target target = hitCollider.transform.GetComponent();
Rigidbody rb = hitCollider.transform.GetComponent();
if(rb != null)
{
rb.AddExplosionForce(10f,gameObject.transform.position,20f,4f,ForceMode.Impulse);
}
if(target != null)
{
target.TakeDamage(damage);
}
Destroy(gameObject);
}
}
}
↧