- Home /
Detection of 2d collision side behaving weird
I am making a basic Breakout game and using the following code to detect if the collision between the Capsule Collider 2d and circle collider 2d has happened on the top:
bool FindIfTopCol(Collision2D other)
{
print("collider.y " + collidersize.y / 2);
ContactPoint2D[] contacts = other.contacts;
if (contacts[0].point.y - transform.position.y > collidersize.y / 2)
{
print("top " + (contacts[0].point.y - transform.position.y));
return true;
}
else
{
print("not top " + (contacts[0].point.y - transform.position.y));
return false;
}
}
For the most part the detection was working fine but after running the game a while and especially after the circle collider gets into a non top collision, the method seems to return all the collisions as non top collision.
To figure out the issue i placed print statements and this is the result:
collidery 0.2610874 top 0.2885695
collidery 0.2610874 not top 0.2552783 First actual non top collision
collidery 0.2610874 not top 0.2542975 It's a top collision, but shows as not top
collidery 0.2610874 not top 0.2558844 It's top collision, but shows as not top
And the rest of the collisions for the session is also wrong like this, until i restart the game.
I am not sure whats going wrong here.
And also if there is a better way to detect collision side, please let me know.
Your answer
Follow this Question
Related Questions
How do I check that no collisions have a tag? 1 Answer
Unity2D: How to make my player not walk through walls? 1 Answer
Physics2D.CircleCast result doesn't match with actual circle object's collision detection 0 Answers
Collider Is Touching Layers is not working... 1 Answer
Disable collision before collision actually happens 1 Answer