/// <summary> /// Override this method to add custom collision logic /// </summary> /// <param name="collision"></param> protected virtual void OnCollisionEnter2D(Collision2D collision) { string tag = collision.transform.tag; if (tag == "Paddle") { float help = collision.transform.GetComponent <Rigidbody2D>().velocity.y; if (help > 0) { body.velocity = new Vector2(body.velocity.x, body.velocity.y + 0.5f); } else if (help < 0) { body.velocity = new Vector2(body.velocity.x, body.velocity.y - 0.5f); } } else if (tag == "Wall") { // Prevent sticking to the wall if (transform.position.y >= 0 && Mathf.Abs(body.velocity.y) <= 1.5f) { // Hit top wall with low velocity - launch down body.velocity = new Vector2(body.velocity.x, -1.5f); } else if (transform.position.y <= 0 && Mathf.Abs(body.velocity.y) <= 1.5f) { // Hit bottom wall with low velocity - launch up body.velocity = new Vector2(body.velocity.x, 1.5f); } } manager.PlaySound(tag); }
// Override this method for custom the goal trigger protected virtual void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "Ball") { manager.PlaySound("Goal"); // Hit by ball manager.scoreGoal(onLeft); } }