Example #1
0
    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Wall"))
        {
            //Debug.Log(bl.velocity);
            //Debug.Log(Vector3.Reflect(bl.velocity, collision.contacts[0].normal));
            //Debug.Log(rg.velocity);
            Vector3 velocity = Vector3.Reflect(bl.velocity, collision.contacts[0].normal);
            rg.AddForce(velocity);
            //Debug.Log(rg.velocity);
        }
        else if (gameObject.CompareTag("Player") && managerScript.state == LevelManager.GameState.Shooting)
        {
            rg.velocity      = Vector3.zero;
            rg.constraints   = RigidbodyConstraints.FreezeAll;
            gameObject.layer = 9;
            gameObject.tag   = "BallsInTube";
            FindNearestPoint();

            if (collision.gameObject.CompareTag("BallsInTube"))
            {
                Ball otherBall = collision.gameObject.GetComponent <Ball>();

                if (collision.transform.position.x > transform.position.x)
                {
                    managerScript.ballsInLine.Insert(otherBall.ballPosition, gameObject);
                    ballPosition = otherBall.ballPosition;
                }
                else
                {
                    managerScript.ballsInLine.Insert(otherBall.ballPosition + 1, gameObject);
                    ballPosition = otherBall.ballPosition + 1;
                }
                gameObject.transform.SetParent(collision.gameObject.transform.parent);
                FindNearestPoint();
            }
            if (collision.gameObject.CompareTag("Tube"))
            {
                if (managerScript.ballsInLine.Count == 0)
                {
                    managerScript.ballsInLine.Add(gameObject);
                    ballPosition = 0;
                }
                else if (managerScript.ballsInLine[managerScript.ballsInLine.Count - 1].transform.position.x < transform.position.x)
                {
                    managerScript.ballsInLine.Add(gameObject);
                    ballPosition = managerScript.ballsInLine.Count;
                }
                else if (managerScript.ballsInLine[0].transform.position.x > transform.position.x)
                {
                    managerScript.ballsInLine.Insert(0, gameObject);
                    ballPosition = 0;
                }
                gameObject.transform.SetParent(collision.gameObject.transform);
            }
            managerScript.state = LevelManager.GameState.Balancing;
            tube.BalanceBalls();
        }
    }