I have a problem with my Health script. My intension was to when the players falls from a certain height to the ground the player decrease in health… But with this script I’m using it’s not quite working and I can’t figure out why.
If I’m on a platform higher than the heightThreshold and jump down to another platform, or jump UP to another one for that matter, I lose health…
My platform is 6 units above the ground, the platform below is 5 units above the ground - so the difference is 1 unit = No FallDamage should be applied… but when I jump to the lower platform I lose health.
And the same if I jump up to another platform higher than the one at 6 units up…
using UnityEngine;
/// <summary>
/// Class responsible for controlling player's health
/// </summary>
public class HealthController : MonoBehaviour {
[HideInInspector] public float CurrentHP;
public float maxHP = 100f;
public bool regenerate;
public float regenerateSpeed = 5f;
public float delayToRegenerate = 5f;
private float nextRegenerationTime = 0f;
public CameraAnimations cameraAnim;
[Space()]
public AudioClip heartBeat;
[Header("Fall Damage")]
public float heightThreshold = 5f;
public float damageMultiplier = 2.2f;
public AudioManager audioManager;
private float higherHeight = 0f;
private FPSCE_Controller mController;
private CapsuleCollider capsuleCol;
private PlayerUI ui;
private void Start()
{
mController = GetComponent<FPSCE_Controller>();
capsuleCol = GetComponent<CapsuleCollider>();
ui = GetComponent<PlayerUI>();
CurrentHP = maxHP;
}
private void Update()
{
CheckFalling();
CurrentHP = Mathf.Clamp(CurrentHP, 0, maxHP);
audioManager.isDying = CurrentHP < maxHP * .35f;
if (CurrentHP < maxHP * .3f)
audioManager.PlayHeartbeatSound(heartBeat, CurrentHP, maxHP * .3f);
if (regenerate && CurrentHP < maxHP && CurrentHP > 0 && nextRegenerationTime < Time.time)
CurrentHP += Time.deltaTime * regenerateSpeed;
}
public void FallDamage(float damage)
{
ApplyDamage(damage);
cameraAnim.FallShake();
}
private void ApplyDamage(float damage)
{
if(CurrentHP > 0 && damage > 0)
{
CurrentHP -= damage;
}
nextRegenerationTime = Time.time + delayToRegenerate;
}
private void CheckFalling()
{
if(mController.m_Controller.isGrounded && higherHeight > 0f)
{
if(higherHeight > heightThreshold)
{
FallDamage(Mathf.Round(damageMultiplier * -Physics.gravity.y * (higherHeight - heightThreshold)));
}
higherHeight = 0f;
} else if(!mController.m_Controller.isGrounded)
{
if (CheckDistanceBelow() > higherHeight)
higherHeight = CheckDistanceBelow();
}
}
public float CheckDistanceBelow()
{
RaycastHit hit = new RaycastHit();
if (Physics.SphereCast(transform.position, capsuleCol.radius, Vector3.down, out hit, heightThreshold * 10))
return hit.distance;
else
return heightThreshold * 10;
}
}