- Home /
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.
Hello @bunnynsnake, I've just read your scripts and as far as I can initially tell they seem to be in working conditions. I didn't not understand your question, however. What exactly do you want this "DecreaseHeartRateEvent" to do?
I want the players heart rate to increase by an increment of 20 if they are jumped by an enemy. The DecreaseHeartRateEvent just lowers the players heart rate over time while they are not being chases or being jumped.
I know its a lot of code, I want to know how to get a static bool to increment the players bpm by 20. That is pretty much it. I mean it would be nice to know if i should be using one code and not two different ones. But i'll take the static bool question.
Answer by Ramlock · Mar 28, 2018 at 04:53 AM
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?
Thank you. This should be what i need for now. But i will have following questions as there are many other things i am still trying to figure out with this code.
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I stop jumping in midair? 4 Answers
Need help setting up Simple_Swordman by Black Hammer 0 Answers
Player Isn't Moving 0 Answers