C# coding help

I am trying to do a game where the player’s heart rate is raised on screen when they enter a collider of a monster, and over time it decreases. I was thinking that just a text on screen with a UI element, that displays a float variable would work.

so basically, the heart rate would start at something like 64bpm and then when the player gets near a monster the heart rate would raise to something like 75bpm. then, when the heart rate gets to a certain point, maybe 120pbm, the player has a heart attack and its game over.

I am a little confused on how to get all this work. I know this is quite a loaded question. But It was better to do this all in one rather than segmenting it. I really need help with the coding with this whole project.

any help is welcomed.

Thank You

Ok so ignore the collider part of it. use distance instead.

float heartRateBase = 100; // 100 BPM
public float heartRate = 100;

float safeDistance = 15; // Where heart rate goes down

float relaxRate = 1f; // sets how fast the player recovers
float stressRate =1f;// The closer it is to the monster the more it will increase

void FixedUpdate () {
// Find distance from monster
float dist = Vector3.Distance (player, monster);

// Check to see if player is safe
if (dist > safeDistance ) {
// Decrease player heart Rate
if (heartRate > heartRateBase )
  heartRate -= relaxRate ;
} else {
// Increase the players heart rate depending on how close he/she is
 float rate = 1 - dist/safeDistance;
heartRate += stressRate * rate;


I don’t have unity on this PC so lett me if its works.