Collider Detection Problem

3270914--252782--1Picture.png 3270914--252783--2Picture.png 3270914--252784--3Picture.png

Hi , i am using this script with contactPoint for collider detection.
This sicript makes it work as in the picture.When my cannon balls hit the collider as in the second picture it detects right and bottom for this my cannon ball reflect wrong way.How can i fix this script to be like third picture ?

void OnCollisionEnter2D(Collision2D col){

Collider2D collider = col.collider;

Vector3 contactPoint = col.contacts [0].point;
Vector3 center = collider.bounds.center;

bool right = contactPoint.x > center.x;
bool left = contactPoint.x < center.x;

bool top = contactPoint.y > center.y;
bool bottom = contactPoint.y < center.y;

if (right) {
transform.rotation = Quaternion.Inverse (col.transform.rotation);
Debug.Log (“Right”);
} else if (left) {
Debug.Log (“Left”);
transform.rotation = Quaternion.Inverse (transform.rotation);
}

if (top) {
transform.rotation = Quaternion.Euler (new Vector3 (1, 1, -180)) * Quaternion.Inverse (transform.rotation);
Debug.Log (“Top”);
} else if (bottom) {
transform.rotation = Quaternion.Euler (new Vector3 (1, 1, 180)) * Quaternion.Inverse (transform.rotation);
Debug.Log (“Bottom”);
}

transform.rotation = Quaternion.Inverse (transform.rotation);

}
}
}

As I understand, you want to detect if the ball is just on the right OR on the left side? In this case you just need to take care about the x axis.

Just use :

bool right = contactPoint.x > center.x;
if (right)
{
// the ball is in the right area
}
else
{
// the ball is in the left area
}

~Nyudeb

i wrote my problem a bit wrong.I edited my post