// Update is called once per frame
    public virtual void Update()
    {
        if (health <= 0)
        {
            isDead = true;
        }

        healthHud.SetHealth(health);

        //if (GamePad.GetButtonDown(GamePad.Button.Back, gamePadIndex))
        //{
        //    Camera.main.GetComponent<AdvancedCamera>().Zoom(new Vector2(gameObject.transform.position.x, gameObject.transform.position.y));
        //}

        if (gameObject.transform.parent.gameObject.GetComponent <Player>().kills >= 5)
        {
            Global.WorldObject.GameWon(controllerNumber, chosenCharacter);
            //Global.WorldObject.winnerIndex = chosenPlayerIndex;
        }

        if (!IsDead && !isDrinking && !advancedCamera.asleep)
        {
            #region ATTACKING CODE


            if ((GamePad.GetButtonDown(GamePad.Button.X, gamePadIndex) ||
                 (useMpu && mpuController.GetDigitalPressed(9))) &&
                onGround && !isDrinking)
            {
                if (playerScript.usePunchbag)
                {
                    playerScript.punchBag.GetComponent <Punchbag>().Hit(gameObject.transform.position);
                    if (punchCheck.collider2D.bounds.Intersects(playerScript.punchBag.collider2D.bounds))
                    {
                        playerScript.punchBag.GetComponent <Punchbag>().Hit(gameObject.transform.position);
                        print("check");
                    }
                }

                foreach (GameObject d in Global.drinks.ToArray())
                {
                    Drink drinkScript = d.GetComponent <Drink>();

                    if (bodyCheck.collider2D.bounds.Intersects(drinkScript.drinkCollision.collider2D.bounds))
                    {
                        sortOfDrink = drinkScript.drinkType;
                        GetDrunk(sortOfDrink);
                        drinkScript.Remove();

                        isDrinking = true;
                        playerObject.rigidbody2D.velocity = Vector2.zero;
                        SetAnimation("isDrinking_1", true);
                        SetAnimation("isDrinking_2", true);
                        SetAnimation("typeOfDrink", drinkScript.drinkNumber);
                        //Invoke("NotDrinking", .4f);
                    }
                }
            }


            if ((GamePad.GetButtonDown(GamePad.Button.X, gamePadIndex) ||
                 (useMpu && mpuController.GetDigitalPressed(9))) && onGround &&
                attackOnce == -1 && !isDrinking && attackCooldown == 0)
            {
                //playerState = PlayerStates.ATTACKING;
                isAttacking = true;
                SetAnimation("isAttacking", true);
                attackOnce     = 1;
                attackCooldown = attackCooldownMax;

                bool didHit = false;

                foreach (GameObject lep in Global.leprechauns.ToArray())
                {
                    Player p = lep.gameObject.transform.parent.GetComponent <Player>();
                    if (punchCheck.collider2D.bounds.Intersects(p.GetCollisionObject("bodyCheck", lep).collider2D.bounds) && lep != gameObject)
                    {
                        if (p.GetLeprechaunScriptType().GetType() == typeof(Leprechaun))
                        {
                            Leprechaun lepScript = (Leprechaun)p.leprechaunScript;
                            lepScript.GotHit(gameObject.transform.position, damageMultiplayer, gamePadIndex, punchShakeHardness);
                            didHit          = true;
                            lepScript.isHit = true;
                            lepScript.SetAnimation("isHit", true);
                            lepScript.Invoke("NotHit", .2f);
                        }
                        else if (p.GetLeprechaunScriptType().GetType() == typeof(Leprechaun_USA))
                        {
                            Leprechaun_USA lepScript = (Leprechaun_USA)p.leprechaunScript;
                            lepScript.GotHit(gameObject.transform.position, damageMultiplayer, gamePadIndex, punchShakeHardness);
                            didHit          = true;
                            lepScript.isHit = true;
                            lepScript.SetAnimation("isHit", true);
                            lepScript.Invoke("NotHit", .2f);
                        }
                        else if (p.GetLeprechaunScriptType().GetType() == typeof(Cluirichaun))
                        {
                            Cluirichaun lepScript = (Cluirichaun)p.leprechaunScript;
                            lepScript.GotHit(gameObject.transform.position, damageMultiplayer, gamePadIndex, punchShakeHardness);
                            didHit          = true;
                            lepScript.isHit = true;
                            lepScript.SetAnimation("isHit", true);
                            lepScript.Invoke("NotHit", .2f);
                        }
                        else if (p.GetLeprechaunScriptType().GetType() == typeof(FarDarrig))
                        {
                            FarDarrig lepScript = (FarDarrig)p.leprechaunScript;
                            lepScript.GotHit(gameObject.transform.position, damageMultiplayer, gamePadIndex, punchShakeHardness);
                            didHit          = true;
                            lepScript.isHit = true;
                            lepScript.SetAnimation("isHit", true);
                            lepScript.Invoke("NotHit", .2f);
                        }
                        else if (p.GetLeprechaunScriptType().GetType() == typeof(Fairy))
                        {
                            Fairy lepScript = (Fairy)p.leprechaunScript;
                            lepScript.GotHit(gameObject.transform.position, damageMultiplayer, gamePadIndex, punchShakeHardness);
                            didHit          = true;
                            lepScript.isHit = true;
                            lepScript.SetAnimation("isHit", true);
                            lepScript.Invoke("NotHit", .2f);
                        }
                    }
                }

                if (!didHit)
                {
                    int i = UnityEngine.Random.Range(1, 6);
                    gameObject.Stab(Resources.Load("Audio/SFX/Punch_Miss_" + i.ToString()) as AudioClip, 1f, 1f, 0f);
                }
                else
                {
                    int i = UnityEngine.Random.Range(1, 6);
                    gameObject.Stab(Resources.Load("Audio/SFX/Punch_Hit_" + i.ToString()) as AudioClip, 1f, 1f, 0f);
                }
            }

            if (attackOnce >= 1)
            {
                attackOnce -= 1;
            }
            else if (attackOnce == 0)
            {
                attackOnce = -1;
                attackDone = false;

                isAttacking = false;
                SetAnimation("isAttacking", false);
            }

            if (attackCooldown > 0)
            {
                attackCooldown--;
            }
            else if (attackCooldown < 0)
            {
                attackCooldown = 0;
            }

            #endregion

            #region BLOCKING CODE

            //if (GamePad.GetButtonDown(GamePad.Button.B, gamePadIndex) && !isDashing && !isAttacking && !isDrinking)
            //{
            //    isBlocking = true;
            //    SetAnimation("isBlocking", true);
            //}

            //if (GamePad.GetButtonUp(GamePad.Button.B, gamePadIndex))
            //{
            //    isBlocking = false;
            //    SetAnimation("isBlocking", false);
            //}

            #endregion

            #region DASHING CODE
            //if ((GamePad.GetTrigger(GamePad.Trigger.LeftTrigger, gamePadIndex) > 0.1 || GamePad.GetTrigger(GamePad.Trigger.LeftTrigger, gamePadIndex) > 0.1)
            //    && !isDashing && !isDashCooldown)
            //{
            //    isDashing = true;
            //    dashTimer = 0;
            //    //animation.GetAnimation.Start();
            //}

            //if (isDashing)
            //{
            //    if (dashTimer > 10)
            //    {
            //        dashCooldown = 120;
            //        isDashCooldown = true;
            //        isDashing = false;
            //    }

            //    //playerState = PlayerStates.DASHING;
            //    dashTimer++;
            //    Dash();
            //}

            //if (isDashCooldown)
            //{
            //    if (dashCooldown > 0)
            //        dashCooldown--;
            //    else
            //        isDashCooldown = false;
            //}
            #endregion

            #region JUMPING CODE

            if (onGround && useMpu && !isAttacking && !isHit)
            {
                if (mpuController.GetSensor(MPUController.Axis.Y, MPUController.Side.NEGATIVE))
                {
                    SetAnimation("grounded", false);
                    playerObject.rigidbody2D.AddForce(new Vector2(0, maxVelocity.y) / 5);
                }
            }
            else if (onGround && (GamePad.GetButtonDown(GamePad.Button.A, gamePadIndex) || GamePad.GetKeyboardKeyDown(KeyCode.Space)) && !isAttacking && !isHit)
            {
                SetAnimation("grounded", false);
                playerObject.rigidbody2D.AddForce(new Vector2(0, maxVelocity.y));
            }

            if (!onGround && chosenCharacter == Player.Character.FAIRY && canDoubleJump && useMpu && !isAttacking && !isHit)
            {
                if (mpuController.GetSensor(MPUController.Axis.Y, MPUController.Side.NEGATIVE))
                {
                    Vector3 vel = playerObject.rigidbody2D.velocity;
                    vel.y = 0;
                    playerObject.rigidbody2D.velocity = vel;
                    playerObject.rigidbody2D.AddForce(new Vector2(0, maxVelocity.y / 1.2f));
                    canDoubleJump = false;
                }
            }
            else if (!onGround && chosenCharacter == Player.Character.FAIRY && canDoubleJump &&
                     (GamePad.GetButtonDown(GamePad.Button.A, gamePadIndex) || GamePad.GetKeyboardKeyDown(KeyCode.Space)) &&
                     !isAttacking && !isHit)
            {
                Vector3 vel = playerObject.rigidbody2D.velocity;
                vel.y = 0;
                playerObject.rigidbody2D.velocity = vel;
                playerObject.rigidbody2D.AddForce(new Vector2(0, maxVelocity.y / 1.2f));
                canDoubleJump = false;
            }

            #endregion
        }
        else if (isDrinking)
        {
            if (bottom_animator.GetCurrentAnimatorStateInfo(0).IsName("Drink") &&
                bottom_animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 &&
                !bottom_animator.IsInTransition(0))
            {
                NotDrinking(1, false);
            }

            if (bottom_animator.GetCurrentAnimatorStateInfo(0).IsName("Drink") &&
                bottom_animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1 &&
                !bottom_animator.IsInTransition(0))
            {
                NotDrinking(2, true);
            }
        }


        //ManageDrunkness();

        if (IsDead && deathCounter == 0)
        {
            SetAnimation("isDead", true);
            SetAnimation("grounded", true);
            SetAnimation("isHit", false);
            SetAnimation("isAttacking", false);
            SetAnimation("isBlocking", false);
            SetAnimation("isDrinking_1", false);
            SetAnimation("isDrinking_2", false);
            dustOnce = false;
            dustParticle.GetComponent <Animator>().SetBool("triggerOnce", false);
            gameObject.Stab(Resources.Load("Audio/SFX/Knockout") as AudioClip, 1f, 1f, 0f);

            healthHud.SetHealth(0);
            deathCounter++;
        }
        else if (IsDead && deathCounter == 1 &&
                 bottom_animator.GetCurrentAnimatorStateInfo(0).IsName("KnockOut"))
        {
            SetAnimation("deathCounter", deathCounter);
            SetAnimation("isDead", false);
            Global.leprechauns.Remove(gameObject);

            deathCounter++;
        }
        else if (IsDead && deathCounter == 2)
        {
            RespawnButton();
            Camera.main.GetComponent <AdvancedCamera>().ZoomWithDelay(new Vector2(gameObject.transform.position.x, gameObject.transform.position.y), .1f);
            deathCounter++;
        }
        else if (IsDead && deathCounter == 3)
        {
            if (GamePad.GetButtonDown(GamePad.Button.X, gamePadIndex) ||
                (useMpu && mpuController.GetDigitalPressed(9)))
            {
                respawnButton.GetComponent <RespawnButton>().RemoveRespawnButton();
                playerScript.ResetPlayer(gameObject);
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (!characterSelected)
        {
            //print(mpuController.GetSensorValue(MPUController.Axis.X));


            if (useMPU && tempCom != "COM0")
            {
                if (mpuController.GetSensor(MPUController.Axis.X, MPUController.Side.POSITIVE) && !pressOnce)
                {
                    NextCharacter(-1);
                }
                else if (mpuController.GetSensor(MPUController.Axis.X, MPUController.Side.NEGATIVE) && !pressOnce)
                {
                    NextCharacter(1);
                }
                else if (!mpuController.GetSensor(MPUController.Axis.X, MPUController.Side.NEGATIVE) && !mpuController.GetSensor(MPUController.Axis.X, MPUController.Side.POSITIVE) && pressOnce)
                {
                    pressOnce = false;
                }

                if (mpuController.GetDigitalPressed(9))
                {
                    SelectCharacter();
                }

                //print(mpuController.GetDigital(9));
            }
            else if (useKeyboard)
            {
                if (Input.GetKeyDown(KeyCode.LeftArrow))
                {
                    NextCharacter(-1);
                }
                else if (Input.GetKeyDown(KeyCode.RightArrow))
                {
                    NextCharacter(1);
                }

                if ((Input.GetKeyUp(KeyCode.LeftArrow) || Input.GetKeyUp(KeyCode.LeftArrow)) && pressOnce)
                {
                    pressOnce = false;
                }

                if (Input.GetKeyDown(KeyCode.Space))
                {
                    SelectCharacter();
                }
            }
            else
            {
                if (GamePad.GetAxis(GamePad.Axis.LeftStick, player).x < -.3f && !pressOnce)
                {
                    NextCharacter(-1);
                }
                else if (GamePad.GetAxis(GamePad.Axis.LeftStick, player).x > .3f && !pressOnce)
                {
                    NextCharacter(1);
                }
                else if (GamePad.GetAxis(GamePad.Axis.LeftStick, player).x > -.3f && GamePad.GetAxis(GamePad.Axis.LeftStick, player).x < .3f && pressOnce)
                {
                    pressOnce = false;
                }

                if (GamePad.GetButtonDown(GamePad.Button.A, player))
                {
                    SelectCharacter();
                }
            }


            if (offset != offset_temp)
            {
                offset = Vector2.Lerp(offset, offset_temp, Time.deltaTime * hardness);
                if (Vector2.Distance(offset, offset_temp) < .001f)
                {
                    offset = offset_temp;
                }
            }
            else if (resettingScale)
            {
                resettingScale = false;
            }

            if (scale != scale_temp)
            {
                scale = Vector2.Lerp(scale, scale_temp, Time.deltaTime * hardness);
                if (Vector2.Distance(scale, scale_temp) < .001f)
                {
                    scale = scale_temp;
                }
            }

            if (resettingPos && Vector3.Distance(gameObject.transform.position, startPos) > 0.0001f)
            {
                gameObject.transform.position = Vector3.Lerp(gameObject.transform.position, startPos, Time.deltaTime * hardness);
            }
            else if (resettingPos && Vector3.Distance(gameObject.transform.position, startPos) < 0.0001f)
            {
                resettingPos = false;
                gameObject.transform.position = startPos;
            }

            if (selectedCharacter > numberOfCharacters)
            {
                selectedCharacter = 1;
            }
            else if (selectedCharacter < 1)
            {
                selectedCharacter = 5;
            }
        }
        else if (useMPU && mpuController.GetDigitalPressed(9))
        {
            selectButton.SetActive(false);
            characterSelected = false;
            GameObject.FindGameObjectWithTag("Global").GetComponent <MenuToGame>().playerCharacter[playerIndexInt - 1] = 0;
        }
        else if (useKeyboard && Input.GetKeyDown(KeyCode.Space))
        {
            selectButton.SetActive(false);
            characterSelected = false;
            GameObject.FindGameObjectWithTag("Global").GetComponent <MenuToGame>().playerCharacter[playerIndexInt - 1] = 0;
        }
        else if (GamePad.GetButtonDown(GamePad.Button.A, player) || GamePad.GetButtonDown(GamePad.Button.B, player))
        {
            selectButton.SetActive(false);
            characterSelected = false;
            GameObject.FindGameObjectWithTag("Global").GetComponent <MenuToGame>().playerCharacter[playerIndexInt - 1] = 0;
        }

        sprite.mainTextureOffset = offset;
        sprite.mainTextureScale  = scale;
    }