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.