C# issue --- code assistance.

Good Moring,

I am trying to code a function in my game where the player gets jumped scared by an enemy and the heart rate is raised.

so far,i just have the player getting close to the enemy and it raises and when they get away from the enemy it decreases.

I need a jump in heart rate when the player gets scared. like for instance, in the picture i just have basic objects laid out in a hall. the white cylinders is the enemies and the blue sphere is the player. what im trying to code is, when the player walks in the field of view of the enemy, the enemy rushes at the player and then disappears, causing a raise in heart rate.

This ideally needs to be realistic in a sense, where the player starts at 80 bpm, and then they get scared and it jumps to 100 bpm - incremented by 20 bpm. then slowly decreases unless they get scared again.

The other thing I want to have some help with is a death function when the players heart rate gets to 300 bpm.

and finally, i would like to display the heart rate on the screen, using a UI element.

here is the code i have so far – with comments

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class HeartRate : MonoBehaviour {
 
     public GameObject Player;
     public GameObject Enemy;
     float heartRateBase = 80; // 80 BPM
     float heartRate = 100;
 
     float safeDistance = 5; // distance away form the enemy where heart rate goes down
 
     float relaxRate = 0.005f; //  how fast the player recovers
     float stressRate = 0.05f;// The closer the plauer is to the enemy the more it will increase
 
     void FixedUpdate()
     {
         // Find distance from enemy
         float dist = Vector3.Distance (Player.transform.position, Enemy.transform.position);
        
         // 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 they are
             float rate = 1 - dist / safeDistance;
             heartRate += stressRate * rate;
         }
 
     }
 }

Thank you for any help you guys can offer, I really appreciate it - i know this is a lot to ask for, but i thought it would be best this way, rather than in small sections.

Right well, first I just want to say; go look at Microsoft’s C# stuff and Unity’s C# stuff.
Secondly, after doing that, you need to sit down, grab a pen and paper and think/plan your code out.


Now to answer this:

  1. “…when the player walks in the field of view of the enemy, the enemy rushes at the player and
    then disappears, causing a raise in heart rate.”

    • Use events; (Plan it out according to your unique needs)

PlayerInViewEvent: Enemy will rush towards player and then disappears.

RateIncreaseEvent: Increases heart rate by X amount, according to YourVariable.###
\- 
  1. “…the player starts at 80 bpm, and then they get scared and it jumps to 100 bpm - incremented by 20 bpm. then slowly decreases unless they get scared again.”

    • Make a simple static bool for when events are in progress, if none are(yourBool = false), and your heart rate isn’t 80 (or heart rate > 80) start a DecreaseHeartRateEvent or Method where you -= the heart rate by decrease speed variable.

  2. “…death function when the players heart rate gets to 300 bpm.”

    • if (bpm >= 300)
      {
      *call DeathEvent*
      }

(Events are great! :smiley: )

  1. “…display the heart rate on the screen, using a UI element.”

Carve out several images in Ps or Gimp for various bpm (like every 10 or 20), fill the image with black and then make the heart rate squigglys white (or alpha), then in Unity set grayscale as transparency if you used white or alpha is transparency for that.

-

Then either write a simple shader, write a simple C# script or look into a different way to scroll a plain (horizontal) gradient image behind it and tint that (black&white) (also set grayscale as transparency) image to whatever color you want.

-

Lastly, use a UIMask Component to mask the gradient image with the foreground, heartrate image.

-

In this way, you can display a moving heart rate, as whatever color you want, without any background for that clean HUD look. (or with… whatever.)

For the shader script look at how shaders for water work. They scroll the normal map for the waves, use that same scroll with only one direction on the horizontal axis. Alternatively, simply write a C# script to move the background image in the same way, then repeat, and switch out the mask image when your bpm changes by a tenth or 2. Add in the sound and done.

I’m sure there are other ways, this is simply easiest solution I can think of (get creative!).

I hope you this answer helped you, I wish you the best of luck.
I tried to explain it in the best way I can; you can message me from my profile if you still need help.


Please up vote this answer if you learned something, agree with it, or it worked for you so others can find a solution easier!

-

#####Thank you, ####

#####-RocketFriday#####