/*public GameObject FindNearestEnemy() //Searches all enemies in the scene to find the nearest to the player.
     * {
     *   GameObject[] Gos;
     *   Gos = GameObject.FindGameObjectsWithTag("Enemy"); //Finds all gameobjects tagged "Enemy" in the scene and adds them to the Gos array.
     *   NearestEnemy = null; //Resets the current Nearest enemy in the event that no enemy is detected at all.
     *   float distance = Mathf.Infinity;
     *   Vector3 pos = transform.position; //Player's position at this frame.
     *   foreach (GameObject Enemy in Gos)
     *   {
     *       Vector3 diff = Enemy.transform.position - pos;
     *       float curDist = diff.sqrMagnitude;
     *       if (curDist < distance) //Checks if enemy is closer than previous enemies gone through in the array.
     *       {
     *           NearestEnemy = Enemy;
     *           distance = curDist;
     *       }
     *   }
     *
     *   return NearestEnemy;
     * }*/
    /*public GameObject FindNearestPerch() //Searches for perch point nearest that's in range.
     * {
     *  GameObject[] PerchPoints;
     *  PerchPoints = GameObject.FindGameObjectsWithTag("Perch"); //finds every gameobject tagged as "Perch" in the scene.
     *  NearestPerch = null; //Resets the current nearest in case none are in range at all.
     *  float distance = Mathf.Infinity;
     *  Vector3 pos = transform.position; //Player's position this frame.
     *  foreach (GameObject PerchPoint in PerchPoints)
     *  {
     *      Vector3 diff = PerchPoint.transform.position - pos;
     *      float curDist = diff.sqrMagnitude;
     *      if (curDist < distance) //Narrows down every perch point in the array to the nearest one to the player.
     *      {
     *          //NearestPerch = PerchPoint;
     *          //distance = curDist;
     *          if ((PerchPoint.transform.position.x <= (transform.position.x + PerchDistanceCheck)) && (PerchPoint.transform.position.x >= (transform.position.x - PerchDistanceCheck))) //Checks if the nearest perch point is in range.
     *          {
     *              if ((PerchPoint.transform.position.y <= (transform.position.y + PerchDistanceCheck)) && (PerchPoint.transform.position.y >= (transform.position.y - PerchDistanceCheck)))
     *              {
     *                  NearestPerch = PerchPoint;
     *                  distance = curDist;
     *              }
     *          }
     *      }
     *  }
     *
     *  return NearestPerch;
     * }*/

    public void DamageTaken(int HealthLost) //Public script that is called by anything doing damage when it's currently going to hit the player.
    {
        if (PlayerHealthState != Player_States.PlayerHealthStates.Dead)
        {
            if (DamageEnabled)                       //Checks if player's invulnerable or not.
            {
                HealthScript.HealthLost(HealthLost); //Takes away from current HP in the Health subscript.
                IFramesTimer = DamageInvuln;
                LevelGM.Instance.NoDamage = false;
                DamageTime    = DamageInvuln / 4;
                DamageEnabled = false; //Give player temporary i-frames
                                       //bool HitAnimGo = (HealthScript.DomahdAlive ? true : false); //Checks if Domahd is still out and alive.
                                       //if (HitAnimGo == true) //Plays damage animation for Domahd if it's not dead.
                                       //{
                                       //    DomahdScript.DomahdAnim.SetTrigger("Damaged");
                                       //}
                                       //myAnim.SetTrigger("Hurt");
                                       //myVoiceBox.clip = DamageGrunt;
                                       //myVoiceBox.Play();
                myAnim.SetBool("Hurt", true);
                LevelGM.Instance.ClearMind = false;
                PlayerDebuffState          = Player_States.PlayerDebuffState.Normal;
                //Everything below resets player's states and resets animation booleans and values to cancel out of anything the player is doing while the damage is happening.
                MeleeScript.ResetMeleeState();
                PlayerState   = Player_States.PlayerStates.Damaged;
                ClingToWall   = false;
                myRB.velocity = new Vector2(0, 0);
                myHitBox.SetBool("Crouching", false);
                myAnim.SetBool("Dashing", false);
                myAnim.SetBool("ClearMind", false);
                //myAnim.SetBool("PounceReady", false);
                //myAnim.SetBool("Pounce", false);
                //myAnim.SetBool("Perched", false);
                myRB.constraints            = MovementScript.YContraints[0];
                MovementScript.dashTime     = 0;
                MovementScript.WallJumpTime = 0;
                //CurrentPerch = null;
                //MovementScript.PerchScript = null;
                //PerchToIgnore = null;
                damageSound.Play();
                DamageState(); //Calls the damage state function for the first time.
            }
        }
    }