Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Emit "Explosion" like force from player, that doesn't effect player

Discussion in 'Physics' started by invisibledesign, Oct 4, 2018.

  1. invisibledesign

    invisibledesign

    Joined:
    Jul 25, 2018
    Posts:
    8
    Hello All!

    I'm currently building my first game in unity, i am really enjoying it coming over from xcode and scenekit. In my game, I am trying to emit some kind of force from my player on key press. I'll try and paint the best picture possible here and post my current code below.

    The player is a spaceship, and when you press a key, it triggers a particle system effect that bursts a ring out around the ship. That works great, but now i need to also repel any rigidbody away in a certain radius 360 degrees around the ship. It's like a defence mechanism when things are getting too close to you. This should only fire 1 burst per key down.

    Here is my current code, The force IS working... it's just really really weak. When I log the rigidbody's in the collider loop, it only picks up other body's when you are right up against them, and apply's a very weak impuse push, as in, the other body's only move a few units away. I feel like my `radius` and `power` variables are big enough that they should go flying away dramatically.

    Code (CSharp):
    1.     public ParticleSystem particleSystem;
    2.     public float fireRate;
    3.    
    4.      public float radius = 500.0f;
    5.     public float power = 1000.0f;
    6.  
    7.     private float nextFire;
    8.  
    9.     void Update () {
    10.         if ( Input.GetButtonDown("Fire2") && Time.time > nextFire )
    11.         {
    12.             nextFire = Time.time + fireRate;
    13.             particleSystem.Play();
    14.             FireBurst();
    15.         }
    16.     }
    17.  
    18.     void FireBurst()
    19.     {
    20.         Vector3 explosionPos = transform.position;
    21.         Collider[] colliders = Physics.OverlapSphere(explosionPos, radius);
    22.         foreach (Collider hit in colliders)
    23.         {
    24.             Rigidbody rb = hit.GetComponent<Rigidbody>();
    25.  
    26.             if (rb != null)
    27.                  rb.AddExplosionForce(power, explosionPos, radius, 0f, ForceMode.Impulse);
    28.         }
    29.     }
    Any advice, tutorials, docs you could throw my way would be amazing!
     
    Last edited: Oct 4, 2018
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    OverlapSphere will include your ship. Check the tag of each object and exclude your ship. hit.gameObject.CompareTag("Player") for example.
     
  3. invisibledesign

    invisibledesign

    Joined:
    Jul 25, 2018
    Posts:
    8
    Awesome i never knew about the tags stuff, really nice. I updated my question after i put that in a did a bit more digging... the problem is different.
     
  4. davidfrk

    davidfrk

    Joined:
    Feb 13, 2017
    Posts:
    43
    Use ForceMode.VelocityChange or more impulse force since it takes into account the mass.
     
  5. invisibledesign

    invisibledesign

    Joined:
    Jul 25, 2018
    Posts:
    8
    Nice, i definitely see the change, everybody is affected equally now which is perfect. But the radius & power variables still don't seem to affect the amount of force that is applied. My objects just barely get pushed back. Do you have theories as to why this is?
     
  6. davidfrk

    davidfrk

    Joined:
    Feb 13, 2017
    Posts:
    43
  7. invisibledesign

    invisibledesign

    Joined:
    Jul 25, 2018
    Posts:
    8
    hmmm that doesn't seem to make any difference. really, no matter what i change my radius and power values too, the object affected always is pushed the same very small amount. There has got to be something about how my objects are setup that is messing with this. But it's quite simple at the moment. I have my spaceship, and a sphere, both have rigid bodies and collide with each other properly. The game objects only move on the x and z axis, no Y movement. The ship has a mass of 10 and the sphere has a mass of 1. Does any of that seem like it would affect what i'm trying to do?