This might come off as a stupid question, but in an if statement, is it possible to check if the object is currently colliding with another object?
Here is what I ha
Maybe what you are looking for is the OnCollisionStay method, this one will fire as long as they are touching each other.
There is some more info in the link here: https://docs.unity3d.com/ScriptReference/Collider2D.OnCollisionStay2D.html
Are there different solutions than
OnCollisionEnter2D
Yes. Many of them!
CircleCollider2D
, other 2D colliders and its base class Collision2D
, have built in functions to do this.
public bool IsTouching(Collider2D collider); //Check collision by the provided collider
public bool IsTouchingLayers(); //Check collision by any collision
public bool IsTouchingLayers(int layerMask); //Check collision by layer
public bool OverlapPoint(Vector2 point); //Check collision by position
The first function is more appropriate for this.
Simple Example:
Collision2D collider1;
Collision2D collider2;
void Update()
{
//Checks if collider1 is touching with collider2
if (collider1.collider.IsTouching(collider2.collider))
{
}
}
Example with the OnCollisionEnter2D
function:
public CircleCollider2D myCircleCollider;
void OnCollisionEnter2D(Collision2D c)
{
if (c.collider.IsTouching(myCircleCollider))
{
}
}