[Solved] (Physics 2D) Detect collision between a "Solid" Object and a "Gas" object

Well, As the title says, I need to detect a collision, by “Solid” Object, I mean an Object you collide with and… well you cant go through it.
Graphic Representation

And “Gas” Objects, which you collide with, and you can go straigt trough, but still the collision detects, continously.
Graphic Representation

Now that I think you understand what I mean, lets get to the point:

There are this “Wormholes”, which whenever player touches them, they are “Warped through space and never seen or heard from again” (Its the way you loose), and, they are not saved in the scene, they spawn randomly during runtime, and also there is more than one. to a grand max of 57 Wormholes, so I cant just make a code for 1. Also, there is a multiplayer version and there can be up to 4 players.

So I want the wormholes to have a child empty GameObject, which is like an “Aura of Attraction”, which make the player be attracted to the Wormhole.

This is the code I use to give the Attraction force to the player, it is attached to the “Area of Attraction” gameObject:

   Vector3 attractDirection; //explained by itself
   float force;  //Explained by itself

    void OnCollisionEnter2D(Collision2D coll)
    {
        if (coll.gameObject.tag == "Player") //If its the player
        {
            force = 60;
            Vector2 dir = coll.contacts[0].point - GetComponent<Rigidbody2D>().position; //Calculates the direction the player is
            attractDirection = -dir.normalized;      //The negative of the direction the player is in, cause it is supposed to attract it, not push it away.
            coll.gameObject.GetComponent<Rigidbody2D>().AddForce(attractDirection * force); //Attracts the player.
        }
    }

This works, because if I turn Player’s collider off (Using the script, cause it must be in the same frame the collision occurs, so I just add the line coll.gameObject.GetComponent().enabled = false; ) then the player is pulled inside, however if I turn the player’s collider ON again, it is kicked out of the Aura, cause it acts as a “Solid” object.

Also, In the Aura of Attraction’s rigidbody I set the Collision detection to be “Continous” so I assume it will add a force of 60 every frame

Thanks in advance for any help.

You want your gas area to be a trigger instead of a collider. Then it will be detected with OnTriggerEnter2D and the player will pass through it. Also, this is only triggered when the player first enter, after that you will need to use OnTriggerStay2D().

If you want to add an attraction effect, then it is probably easier to use the PointEffector2D component. This does exactly what you are trying to do, but it will work automatically if you add a collider set as a trigger and the PointEffector2D component.

1 Like

Let me try that, thank you, I was starting to think of every frame raycasting all the players to see if the distance was inside the area I wanted to have “attraction effect”, but that sounds way better.