Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AddExplosionForce moves player in Y direction only?

Discussion in 'Scripting' started by miroslaw, Dec 3, 2012.

  1. miroslaw

    miroslaw

    Joined:
    Nov 15, 2012
    Posts:
    70
    I've noticed on unity3d forums that some people have had issues with AddExplosionForce() only affecting the rigidbody in the Y direction, I am experiencing this myself.

    I'm using it via a network. So, I have a missile and when it explodes it's supposed to add the explosionforce on it's position when it collides with something.

    Now for the odd behaviour. If the client fires a missile at the server player, the server player receives the correct explosion force. But if the server fires the missile at the client player, it explodes on contact, but the explosion only drives the player upwards on their Y direction. This happens on each consecutive client player and looks like only the server receives the true explosion physics.

    Does anyone know why this may be happening?

    Heres my code (attached to the missile):

    Code (csharp):
    1.  
    2. void OnCollisionEnter (Collision collision)
    3. {
    4.  
    5.     transform.rigidbody.isKinematic = true;
    6.  
    7.  
    8.     if (Network.isServer  !isDestroyed){
    9.  
    10.         ContactPoint contact = collision.contacts [0];
    11.         Quaternion rotation = Quaternion.FromToRotation (Vector3.up, contact.normal);
    12.         Vector3 explosionPosition = transform.position;//contact.point;
    13.  
    14.  
    15.  
    16.         networkView.RPC("DestroyRocket", RPCMode.All, explosionPosition , rotation, transform.forward);
    17.         isDestroyed = true;
    18.     }
    19.  
    20.  
    21. }
    22.  
    23. [RPC]
    24. void DestroyRocket(Vector3 explosionPosition, Quaternion rotation, Vector3 dir){
    25.  
    26.  
    27.     Collider[] colliders = Physics.OverlapSphere (explosionPosition, explosionRadius);
    28.  
    29.     foreach (Collider hit in colliders) {
    30.  
    31.         if (hit.rigidbody) {
    32.             hit.transform.rigidbody.AddExplosionForce (explosionPower, explosionPosition, explosionRadius, 3.0f);
    33.         }  
    34.  
    35.  
    36.     }
    37.     Destroy(gameObject);
    38.  
    39. }
    40.  
    41.  
    If anyone has an alternative to using AddExplosionForce() that would also be great.
     
    Last edited: Dec 3, 2012
  2. miroslaw

    miroslaw

    Joined:
    Nov 15, 2012
    Posts:
    70
    updated post with more info
     
  3. miroslaw

    miroslaw

    Joined:
    Nov 15, 2012
    Posts:
    70
    I have a really good feeling that this has something to do with the network... it may be that the missile is inside of the collider, and when it applies the explosion force, it just applies it to the Y-axis. Is there a way where if the projectile is inside of a collider to move it out from the direction it came in? At least I think that's what the problem is..
     
  4. ss_kemper0k

    ss_kemper0k

    Joined:
    Jan 13, 2020
    Posts:
    1
    I have solved almost the same problem, disabling "Apply root motion" on the player's Animator component.