Explosion Force to Player's RigidBody 2D

Hey there,
I’m new to Unity, I know some basics of C# programming language, but it’s not enough to make what I wanted to make. Right now I have a Player which has got movement control, rigidbody of course and the collider as well, + I have a gun attached to him, the gun Fires missiles. I want to make these missiles push my player away / upside, depends on the angle, when they hit anything. Could someone help me with an advice of a code?
Here is what I’ve got right now, it’s an Script written in JS, and it makes the explosion right after the missile spawns, + it pushes all the nearby objects that got RigidBody2D away, but not the player (which I actually need here to be pushed).

#pragma strict

var EXPLOSION_FORCE = 20;

function Start () {

explode();
}

function explode () {
var all_rigidbodies = FindObjectsOfType(Rigidbody2D);

for (var r : Rigidbody2D in all_rigidbodies)
{
if (Vector2.Distance(r.transform.position, transform.position) < 6 && r.tag != “Player”)
{
var px : float = r.transform.position.x - transform.position.x;
var py : float = r.transform.position.y - transform.position.y;

r.AddForce((Vector2(px, py) * 200), ForceMode2D.Force);
}
}
}

I would be really thankfull if anyone could help me with that, maybe something is wrong with this code, and you can help me to correct it. I wouldn’t mind a good script in C# tho.

When placing code in the forums, please use the Insert > Code command, it makes it much easier to read. (The insert button is to the left of the button that looks like an old harddisk on the menu above your reply box)

Anyway, on to your question: If I understand correctly, you want your player to be pushed away when an explosion happens along with all other rigid-bodies. The problem is right now that is not happening.

I’d like to point toward this line right here:

if (Vector2.Distance(r.transform.position, transform.position) < 6 && r.tag != "Player")

If you want your player to be pushed, why are you checking that a RigidBody does not have the “Player” tag?

I am suspecting that you copied this code from another source and used it for your project. Which is absolutely fine, but it’s important to read through your code and understand what it does.

Remove that condition, and I’d expect it to start working:

if (Vector2.Distance(r.transform.position, transform.position) < 6)

Thanks, now the explosion pushes my player away! But the explosion happens right in the moment when the missile spawns, and I need it to happen on collision with the floor / walls / whatever… How can I make that happen?

#pragma strict

var explosionForce = 500;

function Start () {

         explode();
}




function explode () {
var all_rigidbodies = FindObjectsOfType(Rigidbody2D);

for (var r : Rigidbody2D in all_rigidbodies)
{
    if (Vector2.Distance(r.transform.position, transform.position) < 6 )
    {
        var px : float = r.transform.position.x - transform.position.x;
        var py : float = r.transform.position.y - transform.position.y;

         r.AddForce((Vector2(px, py) * explosionForce), ForceMode2D.Force);             
    }
  }
}

I think you might need to take some coding tutorials and get a good grasp of the basics.

If this code is placed on the missile object, it makes perfect sense that it explodes on instantiation, because explode() is called on start()

Creating games is good practice, but it definitely helps to have some foundational skills to practice with!

Okay, I’m working on it.

1 Like

Im not a programmer but I can understand the logic of the problem.

The explosion setup to push the rigid bodies back is being created when the missle is instantionated/created.

You need to move/change the explosion to happen when the missle is destroyed.
Then the force you are calling will effect all the rigid bodies when you want it to.

Thats the logic anyway - now just edit your code to work like that and it will work. :slight_smile: