Esempio n. 1
0
    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.name == "Paddle")
        {
            // (ball position - paddle position) / paddle width
            float x = (_rigidbody2D.position.x - collision.rigidbody.position.x) / collision.collider.bounds.size.x;

            // Calculate direction, set length to 1
            Vector2 dir = new Vector2(x, 1).normalized;

            // Set Velocity with dir * speed
            _rigidbody2D.velocity = dir * _rigidbody2D.velocity.magnitude;

            _audioSource.PlayOneShot(_paddleHitClip);
        }
        else if (collision.gameObject.name == "Tilemap")
        {
            BrickController controller = collision.gameObject.GetComponent <BrickController>();

            controller.HandleBrickCollision(collision, _rigidbody2D.velocity.magnitude);
        }
        else // walls and ceiling
        {
            _audioSource.PlayOneShot(_wallHitClip);
        }
    }
Esempio n. 2
0
    private void OnCollisionEnter2D(Collision2D collision)
    {
        Destroy(gameObject);
        if (collision.gameObject.name == "Tilemap")
        {
            BrickController controller = collision.gameObject.GetComponent <BrickController>();


            controller.HandleBrickCollision(collision, _rigidbody2D.velocity.magnitude);
        }
    }