public void TakeDamage(Transform enemy) { AuxFunctions.HitDirection hitDirection = AuxFunctions.ReturnDirection(enemy, transform); if (!playerControl.isInvulnerable) { anim.SetTrigger("angryHeadbob"); // Make sure the player can't jump. playerControl.jump = false; // Create a vector that's from the enemy to the player with an upwards boost. Vector3 hurtVectorVertical = transform.position - enemy.position + Vector3.up * 5f; Vector3 hurtVectorHorizontal; if (hitDirection == AuxFunctions.HitDirection.Left) { hurtVectorHorizontal = transform.position - enemy.position + Vector3.right * 5f; } else { hurtVectorHorizontal = transform.position - enemy.position + Vector3.left * 5f; } // Cancel prior velocities GetComponent <Rigidbody2D> ().velocity = new Vector3(0, 0, 0); if (playerControl.isGrounded == true) // Add a force to the player in the direction of the vector and multiply by the hurtForce. { GetComponent <Rigidbody2D> ().AddForce(hurtVectorVertical * hurtForceGrounded); GetComponent <Rigidbody2D> ().AddForce(hurtVectorHorizontal * hurtForceGrounded); } else { GetComponent <Rigidbody2D> ().AddForce(hurtVectorVertical * hurtForceAirborne); GetComponent <Rigidbody2D> ().AddForce(hurtVectorHorizontal * hurtForceAirborne); } // Reduce the player's health by 10. health -= damageAmount; if (health <= startingHealth / 2 && !isHurt) { isHurt = true; foreach (SpriteSwitch ss in GetComponents <SpriteSwitch>()) { ss.Switch(); } } lifeBar.UpdateHealthBar(health, true, isHurt); // Play random hurt clip PlayRandomSound(hurtSounds); } }