Im a bit confused on a simple script

Hi,

We’ll I thought it would be simple.

I’m trying to get this blood fade script to work but getting a little confused.

What I’m doing is calling the damage function from the enemy scripts, so when the enemy is shooting the player the blood fades in, then when they are not the update function fades the blood back out, I did have a global variable controlling the fade out in the update function set to false when the enemy ray cast was hitting the player but when two or more enemy was in the scene it was always set to false if one of them was not attacking the player, so I want it all to be set in this script.

So I’m trying to hold back the fade out while the damage function is being called but getting confused how the best way of doing this would be.

#pragma strict

var bloodSplatter : Texture2D;

private var fade : float;
private var gapSet : boolean;

function Update(){
// If the Player is NOT taking damage slowly fade out the blood effect
	if(!gapSet && fade > 0){
		fade = fade - 0.02;
		GapSetBloodSplatter();
    }
}

// Function called from enemy scrips
function damage(healthToDeduct : float){
// If the Player IS taking damage slowly fade in the blood effect
	if(fade < 1){
		fade = fade + healthToDeduct / 100;
    }
}

function GapSetBloodSplatter(){
    gapSet = true;
    yield WaitForSeconds (0.05);
    gapSet = false;
}

function OnGUI(){
	GUI.color.a = fade;
	GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), bloodSplatter);
}

Ok so line by line lets go through this code:

  • gapSet not being initialised will default to FALSE (hopefully you realise this).
  • I can’t see any initialisation of fade to any value and considering its private, it must happen somewhere in the script you’ve provided but it doesn’t (Or maybe it does and you havent provided it).
  • In your update function I think you should wait for fade to be equal to zero before calling GapSetBloodSplatter() (Also make sure it doesnt fall below 0 either).
  • In your GapSetBloodSpatter function maybe it would be good to reset your value of fade to it’s default also.

These are just a few observations that may trigger some ideas.