Exemplo n.º 1
0
 void ScoreGoal()
 {
     owner.points -= 1;
     if (gameM is BaseballManager)
     {
         hud.UpdateScoresBaseball(gameM.initialPoints, (gameM as BaseballManager).batter.health);
     }
     else if (gameM is GameManagerMata)
     {
         GameManagerMata g = gameM as GameManagerMata;
         g.ScoreGoal(owner.team, false);
     }
     else
     {
         hud.UpdateScores();
     }
     aSource.clip = goalClip;
     aSource.Play();
 }
Exemplo n.º 2
0
    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == "Goal")
        {
            //Ignores collision and keeps same direction
            Physics.IgnoreCollision(sc, col.collider);
            rb.velocity = dir * speed;
        }
        else if (col.gameObject.tag == "Player")
        {
            Pong pongHit   = col.gameObject.GetComponent <Pong>();
            int  pongAngle = (int)col.transform.rotation.eulerAngles.y;

            if (pongHit.striking)
            {
                speed *= pongHit.strikePower;
            }
            else
            {
                speed = initialSpeed;
            }

            if (pongAngle == 0)
            {
                x   = hitFactor(transform.position.x, col.transform.position.x, col.collider.bounds.size.x);
                dir = new Vector3(x, 0, -actualDirectionZ).normalized;

                //reflects
                actualDirectionZ = inverseD(actualDirectionZ);
                rb.velocity      = dir * speed;
            }

            else if (pongAngle > 0 && pongAngle < 90)
            {
                //Use X axis influence
                int valueToWork = 90 - pongAngle;

                //Cross Multiply
                float xInfluence = ((valueToWork * 100) / 90) / 100;
                float zInfluence = 1 - xInfluence;

                float resHitFactor = hitFactor(transform.position.x, col.transform.position.x, col.collider.bounds.size.x);

                float calculatedXFactor = resHitFactor * xInfluence;
                float calculatedZFactor = resHitFactor * zInfluence;


                if (calculatedZFactor > 0.0f)
                {
                    dir = new Vector3(calculatedXFactor, 0, -actualDirectionZ * calculatedZFactor).normalized;
                }
                else
                {
                    dir = new Vector3(calculatedXFactor, 0, -actualDirectionZ).normalized;
                }

                actualDirectionZ = inverseD(actualDirectionZ);
                rb.velocity      = dir * speed;
            }

            else if (pongAngle >= 90 && pongAngle < 180)
            {
                //Use Z axis influence
                int valueToWork = 180 - pongAngle;

                //Cross Multiply
                float zInfluence = ((valueToWork * 100) / 90) / 100;
                float xInfluence = 1 - zInfluence;

                float resHitFactor = hitFactor(transform.position.z, col.transform.position.z, col.collider.bounds.size.z);

                float calculatedZFactor = resHitFactor * zInfluence;
                float calculatedXFactor = resHitFactor * xInfluence;

                if (calculatedXFactor > 0.0f)
                {
                    dir = new Vector3(-actualDirectionX * calculatedXFactor, 0, calculatedZFactor).normalized;
                }
                else
                {
                    dir = new Vector3(-actualDirectionX, 0, calculatedZFactor).normalized;
                }


                actualDirectionX = inverseD(actualDirectionX);
                rb.velocity      = dir * speed;
            }

            else if (pongAngle >= 180 && pongAngle < 270)
            {
                //Use X axis influence
                int valueToWork = 270 - pongAngle;

                //Cross Multiply
                float xInfluence = ((valueToWork * 100) / 90) / 100;
                float zInfluence = 1 - xInfluence;

                float resHitFactor = hitFactor(transform.position.x, col.transform.position.x, col.collider.bounds.size.x);

                float calculatedXFactor = resHitFactor * xInfluence;
                float calculatedZFactor = resHitFactor * zInfluence;


                if (calculatedZFactor > 0.0f)
                {
                    dir = new Vector3(calculatedXFactor, 0, -actualDirectionZ * calculatedZFactor).normalized;
                }
                else
                {
                    dir = new Vector3(calculatedXFactor, 0, -actualDirectionZ).normalized;
                }


                actualDirectionZ = inverseD(actualDirectionZ);
                rb.velocity      = dir * speed;
            }

            else if (pongAngle >= 270 && pongAngle < 360)
            {
                //Use Z axis influence
                int valueToWork = 360 - pongAngle;

                //Cross Multiply
                float zInfluence = ((valueToWork * 100) / 90) / 100;
                float xInfluence = 1 - zInfluence;

                float resHitFactor = hitFactor(transform.position.z, col.transform.position.z, col.collider.bounds.size.z);

                float calculatedZFactor = resHitFactor * zInfluence;
                float calculatedXFactor = resHitFactor * xInfluence;

                if (calculatedXFactor > 0.0f)
                {
                    dir = new Vector3(-actualDirectionX * calculatedXFactor, 0, calculatedZFactor).normalized;
                }
                else
                {
                    dir = new Vector3(-actualDirectionX, 0, calculatedZFactor).normalized;
                }


                actualDirectionX = inverseD(actualDirectionX);
                rb.velocity      = dir * speed;
            }

            if (!pongHit.isMid)
            {
                lastHit = pongHit;
            }
            else
            {
                if (lastHit != null)
                {
                    if (pongHit.team != lastHit.team)
                    {
                        gManager.ScoreGoal(pongHit.team, false);
                    }
                    else
                    {
                        gManager.ScoreGoal(pongHit.team, true);
                    }
                    Destroy(this.gameObject);
                }
            }
        }
        else
        {
            speed = initialSpeed;
        }
    }