// invoked when object is destroyed. // void Destroy(){} // invoked when another collider is touching this object collider void OnCollisionEnter2D(Collision2D coll) { BoxCollider2D box = coll.gameObject.GetComponent <BoxCollider2D>(); //bounce off player if (coll.gameObject.tag == "Player") { //reflect Y direction direction.y = -direction.y; //adjust angle of X based on position from center of paddle float diffX = this.gameObject.transform.position.x - coll.gameObject.transform.position.x; float offsetFromCenter = diffX / box.size.x; direction.x += offsetFromCenter * 2; } //bounce off a block if (coll.gameObject.tag == "Block") { BlockScript blockScript = coll.gameObject.GetComponent <BlockScript>(); blockScript.Damage(1); var blockPos = coll.gameObject.transform.position; var ballPos = transform.position; //Debug.Log ("(" + coll.gameObject.transform.position.x + ", " + coll.gameObject.transform.position.y + ") vs. (" + transform.position.x + ", " + transform.position.y + ")"); //left or right collision if (Mathf.Abs(blockPos.y - ballPos.y) < 3.8) { direction.x *= -1; } else { direction.y = -direction.y; } } }