I know I should probably be able to figure how to do this by myself, but for some reason I can’t wrap my brain around it.
I have a function that is in Update() that checks for dropping health percentage. Every time health drops a significant percentage, I want the camera to shake.
My problem here is that it continually gets called, and therefore doesn’t stop shaking after the first time.
How should I fix this?
The function:
function CheckHealthPercentage( healthPercent : float ){
if(healthPercent == 1){
damageLevel = "No";
damageColor = Color(0,1,0);
}
else if(healthPercent < 1 && healthPercent >= 0.8 ){
damageLevel = "Minimal";
damageColor = Color(1,1,0);
cameraController.ShakeCamera( 0.5, 1 );
}
else if(healthPercent < 0.8 && healthPercent >= 0.5 ){
damageLevel = "Moderate";
damageColor = Color(1,0.6,0);
cameraController.ShakeCamera( 1.0, 1 );
}
else if(healthPercent < 0.5 && healthPercent >= 0.2 ){
damageLevel = "Heavy";
damageColor = Color(1,0.2,0);
cameraController.ShakeCamera( 1.5, 1 );
}
else if(healthPercent < 0.2 ){
damageLevel = "Intense";
damageColor = Color(0.6,0.1,0.1);
cameraController.ShakeCamera( 2, 1 );
}
}
The problem here is that it needs to be called only once, but also EVERY TIME a damage level shifts downward. I can’t reset it because it is called on update and will just reset itself every frame 
Kergal
2
easy - create a bool only call the function when bool is true.
at the end of the function - set the bool to false.
example :
void Update() {
if(randombool){
SomeRandomFunction();
}
}
void SomeRandomFunction() {
// insert code here
randombool = false;
}
The concept here is that you want to execute some code only when the state changes. In this case when the damage level changes. Your current code is always executing the code when you’re in the state, not when you change to the state.
In order to check state changes you need to keep the previous state, and the current state. Then, when you want to check for a state change you compare the current state to the previous state. If it’s the same then you know you didn’t change state. If it’s different then you did change state, so you can execute your “state change” code.
Here’s what I would do with your code (with most of it left out, just showing this as an example)…
function CheckHealthPercentage(healthPercent : float)
{
// save current damage level
var previousDamageLevel : string = damageLevel;
if (healthPercent == 1)
{
damageLevel = "No";
}
else if (healthPercent < 1 && healthPercent >= 0.8)
{
damageLevel = "Minimal";
}
// did the damage level change?
if (damageLevel != previousDamageLevel)
{
// TODO : shake it
}
}
My Javascript sucks, so no guarantees of syntax correctness 
GC1983
3
Hmm, you could place a timer in there. Call the boolean when collision is made, and then let the timer run out. Make the timer variable public so you can set for how long you want it to shake. For Example:
float shakeTime;
bool cameraShake;
else if(healthPercent < 1 && healthPercent >= 0.8 ){
damageLevel = "Minimal";
damageColor = Color(1,1,0);
if(cameraShake)
{
shakeTime -= Time.deltaTime
cameraController.ShakeCamera( 0.5, 1 );
if(shakeTime <= 0)
cameraShake = false;
}