[Solved] How do you "bounce" off an explosion?

I have visited many tutorials and seen many resolved questions, but none answer my question, I want the player to bounce off an explosion in the exact opposite direction it collided with it:

Graphic Representation

The explosion is not always there, it happens when the player touches that exploding apparatus, then the exploding apparatus spawns an explosion prefab, that works perfecltly fine, but the explosion (as its bigger than the exploding apparatus) only throws the player out of its collider’s range, instead of far far away as Id like it, the player can hit the exploding apparatus from any of the 360 degrees 2D games provide, so coding 360 lines for every possibility its the last thing I want to do, there must be an easier way to achieve this.

Thanx for any help

You need to add velocity to the player object in the direction you want to push it.

Something like this: Push Object in Opposite Direction of Collision - Questions & Answers - Unity Discussions

void OnCollisionEnter(Collision c)
{
     // force is how forcefully we will push the player away from the enemy.
     float force = 3;
     // If the object we hit is the enemy
     if (c.gameObject.tag == "enemy")
     {
         // Calculate Angle Between the collision point and the player
         Vector3 dir = c.contacts[0].point - transform.position;
         // We then get the opposite (-Vector3) and normalize it
         dir = -dir.normalized;
         // And finally we add force in the direction of dir and multiply it by force.
         // This will push back the player
         GetComponent<Rigidbody>().AddForce(dir*force);
     }
}

This is for a 3D environment but the logic is basically the same for 2D environments.

Let me see how it works

I get an error “Operator ‘-’ is ambiguous on operands of type Vector 2 and Vector3”, I had never seen contacts[0].point, so I dont know how to fix this, I dont even know if the problem is in contacts[0].point xd

I changed Vector 3 to Vector 2 and the error persists.

This is the command right now:

    void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.tag == "Explosion")
        {
            float force = 3;
            Vector2 dir = coll.contacts[0].point - transform.position;
            dir = -dir.normalized;
            GetComponent<Rigidbody2D>().AddForce(dir * force);
        }
     }

Try too use the rigidbody position, as you have a 2D game I assume you also use a rigibbody2D which it’s position property returns a Vector2: Unity - Scripting API: Rigidbody2D.position

You can also create a new Vector2 using the transform.position x and y values.

ok it worked 5/7 xD, with 3 it appeared as it did nothing, with 10 still nothing, so I just derped and made it 1000 and it bounced a bit, then with 10,000 I get what I want, but is so fast it kinda “Teleports” away instead of smooth bounce, I already fixed that but Ima give the code anyway to help whoever visits my thread in the far future.

    private bool explode = false;
    private bool largeExplode = false;
    private float blowDelay;
    private Vector2 blowDirection;
    private int force;

    void Update()
    {
        if (blowDelay < Time.time) { explode = false; largeExplode = false; }
        if (explode) { GetComponent<Rigidbody2D>().AddForce(blowDirection * force); }
        if (largeExplode) { GetComponent<Rigidbody2D>().AddForce(blowDirection * force); }
    }

    void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.tag == "Explode")
        {
            blowDelay = Time.time + 0.3f;
            explode = true;
            force = 10;
            Vector2 dir = coll.contacts[0].point - GetComponent<Rigidbody2D>().position;
            blowDirection = -dir.normalized;
        }
        if (coll.gameObject.tag == "LargeExplode")
        {
            blowDelay = Time.time + 0.3f;
            largeExplode = true;
            force = 100;
            Vector2 dir = coll.contacts[0].point - GetComponent<Rigidbody2D>().position;
            blowDirection = -dir.normalized;
        }
    }

*I have both Explode and LargeExplode cause they come in two different sizes

1 Like