// Update is called once per frame
    void Update()
    {
        if (gameOver)
        {
            return;
        }

        /*
         * // SWITCH TEAMS BY PRESSING Q
         * if (Input.GetKeyUp(KeyCode.Q))
         * {
         *  ChangeTeams();
         * }
         */

        // Select Cube
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hitInfo = new RaycastHit();
            bool       hit     = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
            if (hit && hitInfo.transform.gameObject.tag == "Cube")
            {
                Vector3 cubePos = hitInfo.transform.position;

                // if in bounds, move the cube
                Renderer rend = boundingFloors[currentTeam].GetComponent <Renderer>();

                if (cubePos.x > rend.bounds.min.x &&
                    cubePos.x < rend.bounds.max.x &&
                    cubePos.z > rend.bounds.min.z &&
                    cubePos.z < rend.bounds.max.z)
                {
                    //Debug.Log("CUBE");
                    tempCube = hitInfo.transform.gameObject;
                }
            }
        }
        if (Input.GetMouseButtonUp(0))
        {
            RaycastHit hitInfo = new RaycastHit();
            bool       hit     = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);

            if (hit && hitInfo.transform.gameObject.tag == "Cube")
            {
            }

            if (hit && hitInfo.transform.gameObject.tag == "Cube" && hitInfo.transform.gameObject == tempCube)
            {
                Vector3 cubePos = hitInfo.transform.position;

                // if in bounds, move the cube
                Renderer rend = boundingFloors[currentTeam].GetComponent <Renderer>();

                if (cubePos.x > rend.bounds.min.x &&
                    cubePos.x < rend.bounds.max.x &&
                    cubePos.z > rend.bounds.min.z &&
                    cubePos.z < rend.bounds.max.z)
                {
                    SelectCube(hitInfo.transform.gameObject);
                }
            }

            tempCube = null;
        }

        // Throw Cube
        if (selectedCube != null)
        {
            fireDirection = selectedCube.transform.position - mainCamera.transform.position;
            fireDirection.Normalize();

            if (Input.GetKeyUp(KeyCode.E))
            {
                Debug.Log("FIRE!");

                // Remove joints
                FixedJoint[] joints = selectedCube.GetComponents <FixedJoint>();
                for (int i = 0; i < joints.Length; i++)
                {
                    // Destroy the joints of the cubes connected to selected Cube
                    CubeScript cScript = joints[i].connectedBody.gameObject.GetComponent <CubeScript>();

                    if (cScript.connectedObjects.Contains(selectedCube))
                    {
                        FixedJoint[] fixedJoints = cScript.GetComponents <FixedJoint>();

                        for (int j = 0; j < fixedJoints.Length; j++)
                        {
                            if (fixedJoints[j].connectedBody.gameObject == selectedCube)
                            {
                                Destroy(fixedJoints[j]);
                            }
                        }
                    }

                    Destroy(joints[i]);
                }

                Vector3 shotVector = fireDirection * shotForce;
                selectedCube.GetComponent <Rigidbody>().AddForce(shotVector);

                DeselectCube();

                // Change teams after throwing the cube
                ChangeTeams();
            }
        }
    }