- Home /
Question by
LonelyJenkins · Jan 04, 2021 at 10:17 PM ·
physicscharactercontrollerrigidbody.addforce1st personaddexplosionforce
AddExplosionForce not affecting RigidBodies
I have this simple script applied to a Rocket prefab that is supposed to have it explode on contact and apply the AddExplosionForce method. The issue is that, though everything appears functional (The debug texts all appear as expected in Play mode), the intended targets are virtually unaffected by the explosion regardless of the value of the explosion force. Each of the targets have colliders and rigidbodies (mass set to 1) so I am unsure of what could be the problem. Could it be the fact that the rocket prefab itself is kinematic (due to the environment not having convex colliders)? Thanks to anyone who can help!
public float speed = 5;
public float damageRadius = 5;
public float explosionForce = 1000;
public GameObject explosionFx;
private float gravity = -9.81f;
private Vector3 velocity;
// Update is called once per frame
void Update()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
transform.Translate(velocity * Time.deltaTime);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy") || other.CompareTag("Ground"))
{
Impact();
}
}
void Impact()
{
Instantiate(explosionFx, transform.position, transform.rotation);
Collider[] colliders = Physics.OverlapSphere(transform.position, damageRadius);
Debug.Log(colliders.Length + " colliders found");
foreach (Collider nearbyObject in colliders)
{
Rigidbody rb = nearbyObject.GetComponent<Rigidbody>();
if (rb != null)
{
rb.AddExplosionForce(explosionForce, transform.position, damageRadius, 0, ForceMode.Impulse);
Debug.Log("Rbs detected");
}
}
Destroy(gameObject);
}
Comment