Explosion Force not applying to Crate Parts?

Anyone else having an issue where the crate pieces just aren’t flying apart? My Unit ragdolls work just fine and it’s the same script applied to both pieces. I’ve verified that my gravity settings are set at default. I thought it may have been something I did when I set up the crate, but I downloaded the prefab from the lecture commit and am getting the same behavior.

The Destroyed Crate spawns correctly, but none of the pieces fly out with much force. I’ve even upped the explosionForce value in the script with no discernable difference.

    public void Damage() {
        Transform crateDestroyedTransform = Instantiate(crateDestroyedPrefab, transform.position, transform.rotation);
        Vector3 randomDir = new Vector3(Random.Range(-1f,1f), 0, Random.Range(-1f,1f));
        ApplyExplosionToChildren(crateDestroyedPrefab, 3000f, transform.position + randomDir, 10f);
        
        Destroy(gameObject);
        OnAnyDestroyed?.Invoke(this, EventArgs.Empty);
    }

    private void ApplyExplosionToChildren(Transform root, float explosionForce, Vector3 explosionPosition, float explosionRange) {
        foreach (Transform child in root) {
            if(child.TryGetComponent<Rigidbody>(out Rigidbody childRigidbody)) {
                childRigidbody.AddExplosionForce(explosionForce,explosionPosition,explosionRange);
                Debug.Log("AddedForce!");
            }
            ApplyExplosionToChildren(child, explosionForce, explosionPosition, explosionRange);
        }
    }

I think the problem is here. You are applying the force to the prefab instead of the instantiated transform. Try passing crateDestroyedTransform into the function instead of crateDestroyedPrefab

3 Likes

Unbelievable…I looked at the gitlab code at least 3 times and would have sworn my code was identical. That was indeed the problem. Thanks!!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms