
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);
}
}
}