- Home /
Problem with PlayerHealth and renderer
I have built a playerHealth script and I did when the player loses life, meshrenderer to blink every 1 second but when he receives damage do more than once because when he feels damage and boolean when is activated, I have made is activated and make multiple times instead of a. How can I do to flash as I want? See the code:
var curHealth : int = 70;
var maxHealth : int = 100;
var deadMode = false;
var curProtect = false;
static var damageCost = 10;
function Update () {
if(curHealth >= maxHealth) {
curHealth = maxHealth;
}
if(curHealth <= 0) {
curHealth = 0;
deadMode = true;
}
if(curHealth > 0) {
deadMode = false;
}
protectedMode();
}
function OnTriggerEnter( other : Collider) {
if(other.tag == "Enemy" && !curProtect) {
curProtect = true;
curHealth -= damageCost;
}
}
function protectedMode() {
if(curProtect) {
graphicsSkin = transform.Find("Graphics");
graphicsSkin.renderer.enabled = false;
yield WaitForSeconds(1);
graphicsSkin.renderer.enabled = true;
yield WaitForSeconds(1);
graphicsSkin.renderer.enabled = false;
yield WaitForSeconds(1);
graphicsSkin.renderer.enabled = true;
yield WaitForSeconds(1);
graphicsSkin.renderer.enabled = false;
yield WaitForSeconds(1);
graphicsSkin.renderer.enabled = true;
yield WaitForSeconds(1);
curProtect = false;
}
}
Answer by robertbu · May 27, 2013 at 02:36 PM
You can use InvokeRepeating(). You can use CancelInvoke() to turn the blinking back off:
#pragma strict
function Start() {
InvokeRepeating("BlinkMe",0.0,1.0);
}
function BlinkMe() {
renderer.enabled = !renderer.enabled;
}
Note the last parameter to the InvokeRepeating() is the time between invokes. A smaller number will cause your character to blink faster.
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Can someone explain the destroy () command? 1 Answer
Player Health drops fast, how to pause after damage 0 Answers
press e to speek -1 Answers