- Home /
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);
}
What effect are you currently seeing? I can think of other ways, but I'm not sure that they are any better than this.
So what is happening with this script attached to your player ?
It's just not fading in the affect smoothly as the update is slowing it down because it's trying to fade it back out while the damage function is trying to fade it in, getting mixed results with different enemy, as they deduct health at different rates, terrorist, droid, mech .. ect. So it's not fading in full when the players health is down to 0.
As I said I did try using a global variable in the enemy scripts to stop the fade out in update if the enemy was locked on and shooting the player, but if other enemy was in the area they would set the variable to false.
Answer by SubatomicHero · May 15, 2013 at 11:37 AM
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.
Thanks for the input, gapSet defaults to false in the setting of the variable, then it allows the fading out if fade is > 0 in function update, then it is then set to true in GapSetBloodSplatter no longer allowing the fade until it is reset to false again after 0.05 second, allowing a smooth, not to fast fade of the texture.
Again fade defaults to 0 in the setting of the variable, and never goes below -0.01 or round about because of fade = fade - and fade = fade + being inside if(fade > 0) and if(fade < 0)
The script works but as I read it the if part in function update will slow down the damage function because it's in update and always being called, I know this happens because if I remark it out in update the damage function works more smoothly, so I was just wondering if there was a way to stop the if part in function update while the damage function is being called (from the scrips attached to the enemy)
Don't you want your player to get more damages while he takes some damages from another enemy ?
maybe setting a boolean to true in your damage function and yielding a second or two to allow the damage function to run, then setting that boolean back to false. That boolean can then be checked in your update function.
$$anonymous$$iraSensei .. That is happening because each enemy when shooting the player calls -
playerDamage.GetComponent(PlayerDamage).damage(deductPlayerHealth);
so if 1 or more enemy is shooting the player that gets called from each script attached to each enemy, so deductPlayerHealth set in the inspector depending on what enemy strength will be deducted multiple time so the player will be getting more damage the more enemy attacking.
SubatomicHero .. I've tried that but it stops it running smoothly.