Ejemplo n.º 1
0
        private void OnCollisionEnter2D(Collision2D collision)
        {
            PaddleController paddle = collision.gameObject.GetComponent <PaddleController>();
            WallCollision    wall   = collision.gameObject.GetComponent <WallCollision>();
            BrickCollision   brick  = collision.gameObject.GetComponent <BrickCollision>();

            if (brick != null)
            {
                velocity = Vector3.Reflect(velocity, collision.contacts[0].normal);
                brick.HandleCollision();
                AudioManager.Instance.PlayOneShot(brickSFX, true);
                ParticleManager.Instance.PlayParticlesAtPosition(collision.contacts[0].point);
            }

            if (paddle != null)
            {
                velocity.y *= -1f;

                float distanceToCenter = transform.position.x - paddle.transform.position.x;

                if (distanceToCenter < 0)
                {
                    float t = Mathf.InverseLerp(paddle.transform.position.x,
                                                paddle.transform.position.x - paddle.boxCollider.bounds.extents.x,
                                                transform.position.x);

                    Quaternion angle = Quaternion.Lerp(Quaternion.Euler(0f, 0f, 0f), Quaternion.Euler(0f, 0f, 50f), t);
                    velocity = angle * velocity;
                    velocity.Normalize();
                }
                else
                {
                    float t = Mathf.InverseLerp(paddle.transform.position.x,
                                                paddle.transform.position.x + paddle.boxCollider.bounds.extents.x,
                                                transform.position.x);

                    Quaternion angle = Quaternion.Lerp(Quaternion.Euler(0f, 0f, 0f), Quaternion.Euler(0f, 0f, -50f), t);
                    velocity = angle * velocity;
                    velocity.Normalize();
                }

                AudioManager.Instance.PlayOneShot(wallSFX, true);
            }

            if (wall != null)
            {
                if (wall.wallType == WallType.HORIZONTAL)
                {
                    velocity.x *= -1f;
                }
                else if (wall.wallType == WallType.VERTICAL)
                {
                    velocity.y *= -1f;
                }

                StartCoroutine(camShake.Shake(0.125f, 0.2f));
                AudioManager.Instance.PlayOneShot(wallSFX, true);
            }

            Vector2 contactNormal = collision.contacts[0].normal;

            transform.rotation = Quaternion.Euler(0f, 0f, Mathf.Atan2(contactNormal.y, contactNormal.x) * Mathf.Rad2Deg);
            StartCoroutine(SquashAnim());
            StartCoroutine(HitFlash());
        }