Пример #1
0
        private void Update()
        {
            //ativa efeito do power up
            if (ballCollider != null)
            {
                if (CustomFuncs.MyIntersects(myCollider.bounds, ballCollider.bounds))
                {
                    gameManager.ActivateEffect(type);
                    Destroy(gameObject);

                    //Animações
                    if (type == PowerUpType.FIRE)
                    {
                        fundoFogo.gameObject.SetActive(true);
                        Instantiate(paredeFogoEsquerda, transform.position, Quaternion.identity);
                        Instantiate(paredeFogoDireita, transform.position, Quaternion.identity);

                        //Som
                        AudioSource.PlayClipAtPoint(spreadFireSound, Vector3.zero);
                    }

                    if (type == PowerUpType.ICE)
                    {
                        fundoGelo.gameObject.SetActive(true);
                        Instantiate(pedacosGeloDireita, transform.position, Quaternion.identity);
                        Instantiate(pedacosGeloEsquerda, transform.position, Quaternion.identity);

                        //Som
                        AudioSource.PlayClipAtPoint(iceBreakSound, Vector3.zero);
                        AudioSource.PlayClipAtPoint(spreadIceSound, Vector3.zero);
                    }
                }
            }
        }
Пример #2
0
        private void FixedUpdate()
        {
            bool    collisionHappened = false;
            Vector3 newPosition       = transform.position + velocity * Time.fixedDeltaTime;

            //Reflexão nas paredes
            if (newPosition.y > maxY)
            {
                //Torna colisão pixel perfect
                Vector3 diffVect = newPosition - transform.position;
                transform.position += diffVect * Mathf.Abs((maxY - transform.position.y) / diffVect.y);

                velocity = Vector3.Reflect(velocity, Vector3.down);

                collisionHappened = true;
            }
            if (newPosition.y < minY)
            {
                //Torna colisão pixel perfect
                Vector3 diffVect = newPosition - transform.position;
                transform.position += diffVect * Mathf.Abs((minY - transform.position.y) / diffVect.y);

                velocity = Vector3.Reflect(velocity, Vector3.up);

                collisionHappened = true;
            }
            //Reflexão nas raquetes
            foreach (Collider2D collider in padColliders)
            {
                Bounds newBounds = new Bounds(newPosition, myCollider.bounds.size);

                if (CustomFuncs.MyIntersects(newBounds, collider.bounds))
                {
                    BarraController paddle = collider.gameObject.GetComponent <BarraController>();

                    //Calcula a maior (ou menor) posição x que a bola chegar na qual a raquete ainda possa salvar o ponto
                    Vector3 diffVect         = newPosition - transform.position;
                    int     paddleSide       = (diffVect.x > 0) ? 1 : -1; //diz se a bola está indo para a direita ou esquerda
                    float   paddleHalfLenght = collider.bounds.extents.x;
                    float   ballHalfLenght   = myCollider.bounds.extents.x;
                    float   xSafe            = paddle.transform.position.x + (paddleHalfLenght + ballHalfLenght) * paddleSide;

                    //Torna a colisão pixel perfect
                    float xLimit = paddle.transform.position.x - (ballHalfLenght + paddleHalfLenght) * paddleSide;
                    transform.position += (paddleSide * transform.position.x < xLimit * paddleSide) ?
                                          diffVect * Mathf.Abs((xLimit - transform.position.x) / diffVect.x) :
                                          new Vector3(xLimit - transform.position.x, 0, 0);

                    //Efeito da colisão
                    velocity  = Vector3.Reflect(velocity, paddle.normal);
                    velocity += paddle.velocity * padTransmission + paddle.normal * padBumping;

                    //Som da raquetada
                    switch (gameManager.gameState)
                    {
                    case GameState.NORMAL: AudioSource.PlayClipAtPoint(normalBounceSound, Vector3.zero); break;

                    case GameState.FIRE: AudioSource.PlayClipAtPoint(fireBounceSound, Vector3.zero); break;

                    case GameState.ICE: AudioSource.PlayClipAtPoint(iceBounceSound, Vector3.zero); break;

                    default: break;
                    }

                    gameManager.BounceOcurred();
                    collisionHappened = true;
                    break;
                }
            }

            if (!collisionHappened)
            {
                transform.position = newPosition;
            }

            //Limita velocidade
            float speed = Mathf.Min(velocity.magnitude, terminalSpeed);

            velocity = velocity.normalized * speed;
        }