c# code. static bool.

i would like to increment the players bpm by 20 in the script im working on.

i was thinking that a static bool would work for this where it starts a DecreaseHeartRateEvent -= the current heart rate. I am not sure how to get this to work. here are the 2 codes that i have so far.

if you have any questions about the code, please feel free to ask, and i am always open to improvement if there is a better way to do any functions.

scripts:

here is the script for the enemy, where it moves at the player which should cause the bpm jump.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyScript : MonoBehaviour
{
    public GameObject Enemy;
    public GameObject Player;

    private float Rush = -0.005f; //rate at which the enemy moves at the player

    public enum EnemyState { Nice, Aggresive }

    public EnemyState currentState;

    public int close = 2;


    private void Update()
    {
        CheckStates();
    }

    private void CheckStates()
    {
        if (currentState == EnemyState.Nice)
        {
            // Inactive
        }

        if (currentState == EnemyState.Aggresive)
        {
            Enemy.transform.Translate((Enemy.transform.position - Player.transform.position) * Rush);
            Enemy.GetComponent<Renderer> ().enabled = false;
        }
    }
}
public class EnemyCollider : MonoBehaviour
{
    public EnemyScript masterObject;

    public void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            masterObject.currentState = EnemyScript.EnemyState.Aggresive;
        }
    }

    public void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            masterObject.currentState = EnemyScript.EnemyState.Nice;
        }
    }
}

and here is the players heart rate script where it has the decrease and increase in bpm depending on how close to the enemy the player is.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using System;


public class HeartRate : MonoBehaviour {

    public GameObject Player;
    public GameObject Enemy;
    float heartRateBase = 80; // 80 BPM

    public 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;
        }

    }
}

i am not sure which code to put this in, and i am not sure how to get it to work in the way i need it to. may i please get a little assistance with the coding.

any help is welcomed.

thank you so much.

I am under the impression you want a static bool that consumes itself, simulating an event… If that’s the case, this should do it:

private static bool jumped = false;
public static bool Jumped
{
    get { if(jumped) {jumped=false; return true;} return false; }
    set { jumped = value; }
}

and then on your Update()

private void Update()
{
    if(Jumped)
    {
        heartRate += 20;
    }
}

Is this what you needed? If not, how can I assist you?