Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Rigidbody.AddExplosionForce Is Not Working

Discussion in 'Scripting' started by SimpleGiant, Aug 11, 2015.

  1. SimpleGiant

    SimpleGiant

    Joined:
    Dec 22, 2013
    Posts:
    47
    I copy and pasted the unity API on
    Rigidbody.AddExplosionForce
    and it didn't work, is their anything i have to do to my scene to prep it?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. // Applies an explosion force to all nearby rigidbodies
    5. public class TestExplosion : MonoBehaviour {
    6.     public float radius = 5.0F;
    7.     public float power = 10.0F;
    8.    
    9.     void Start() {
    10.         Debug.Log ("bang");
    11.         Vector3 explosionPos = transform.position;
    12.         Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
    13.         foreach (Collider hit in colliders) {
    14.             Rigidbody rb = hit.GetComponent<Rigidbody>();
    15.            
    16.             if (rb != null)
    17.                 rb.AddExplosionForce(power, explosionPos, radius, 3.0F);
    18.            
    19.         }
    20.     }
    21. }
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,596
    Confirm your objects actually have a Rigidbody component on them. Change Power to 300.
     
  3. SimpleGiant

    SimpleGiant

    Joined:
    Dec 22, 2013
    Posts:
    47
    OK, that the problem, i was using numbers that were way to small to do anything. after changing the power to 300 it moved the block. thank you.
     
  4. Daxawn

    Daxawn

    Joined:
    Dec 6, 2019
    Posts:
    8
    I wanted to get the explosion after a collision and it doesn't work, what do I have to do?
    (I also tried changing power to 300 but keeps not working)

    Code (CSharp):
    1. public class RomperVidrio : MonoBehaviour
    2. {
    3.     public Transform brokenObject;
    4.     public float magnitudeCol, radius, power;
    5.  
    6.     void OnCollisionEnter(Collision collision)
    7.     {
    8.         if (collision.relativeVelocity.magnitude > magnitudeCol)
    9.         {
    10.             Destroy(gameObject);
    11.             Instantiate(brokenObject, transform.position, transform.rotation);
    12.             brokenObject.localScale = transform.localScale;
    13.             Vector3 explosionPos = transform.position;
    14.             Collider[] colliders = Physics.OverlapSphere (explosionPos, radius);
    15.  
    16.             foreach (Collider hit in colliders){
    17.                 Rigidbody rb = hit.GetComponent<Rigidbody>();
    18.                 if (rb != null)
    19.                     rb.AddExplosionForce(power, explosionPos, radius, 3.0f);
    20.      
    21.  
    22.             }
    23.              
    24.  
    25.         }
    26.     }
    27. }
    28.  
     
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Did you already try LaneFox's suggestions posted here 4 years ago? Because an explosive force of 3 is probably going to result in a similar force to a firecracker going off under a bus.
     
    MattThomSA and jacobrutter like this.
  6. Daxawn

    Daxawn

    Joined:
    Dec 6, 2019
    Posts:
    8
    Yep mate, I tried, but it doesn't want to do the thing
     
  7. Smagger

    Smagger

    Joined:
    Aug 18, 2021
    Posts:
    2
    Not working for mee too....any updates??
     
    segant likes this.
  8. BPRICE

    BPRICE

    Joined:
    Mar 1, 2020
    Posts:
    8
    Use Bigger Numbers! I needed 1000 Power and 100 Upward Force...
    Make sure your Rigidbody is NOT Kinematic
     
    jitterware likes this.
  9. SoS_Blocking1

    SoS_Blocking1

    Joined:
    Dec 7, 2023
    Posts:
    1
    I have a similar problem, I am trying to launch my player, but it doesn't get knocked back at all, he has a Rigidbody, capsule collider, and player controller, the knock back number is 1500. it is not kinemetric either