c# - Make Player Flash When Hit -
i'm trying make player flash when hit. have hit detection working. changes red. i'm wondering how make actual flashing occur. here's snippet handles being hit:
if(otherobject.comparetag("minion")) //hit minion { if(hitfromtop(otherobject.gameobject)) //if player jumped on enemy { otherobject.getcomponent<minion>().setmoving(false); //stop moving playsound(killenemysound); //play killing enemy sound jump(); destroy(otherobject.gameobject); //kill minion } else //otherwise, enemy hit player { if(cantakedamage) { changecolor(color.red); losehealth(1); //lose health cantakedamage=false; yield return new waitforseconds(1.5f); //delay taking damage repeatedly changecolor(color.white); cantakedamage=true; } //indicate lost health somehow }
currently, changing red constant (not flashing) , temporary, via yield return new waitforseconds(1.5f);
. put couple of these calls, colour changes in between them, wondering if there neater way. can't first 1 make flash in game.
edit: i've added heisenbug's flash() code follows:
ienumerator flashplayer(float time, float intervaltime) { cantakedamage=false; float elapsedtime = 0f; int index = 0; while(elapsedtime < time ) { debug.log("elapsed time = " + elapsedtime + " < time = " + time + " deltatime " + time.deltatime); mat.color = colors[index % 2]; elapsedtime += time.deltatime; index++; yield return new waitforseconds(intervaltime); } cantakedamage=true; }
and called here:
void losehealth(int damage) { //irrelevant details omitted brevity //the "if" checks if player has run out of health, should not affect else because method called ontriggerenter...see below... else { startcoroutine(flashplayer (1.5f, 0.5f)); } }
and here called (in ontriggerenter):
if(otherobject.comparetag("minion")) //hit minion { if(hitfromtop(otherobject.gameobject)) //if player jumped on enemy { //irrelevant problem } else //otherwise, enemy hit player { if(cantakedamage) { losehealth(1); } //indicate lost health somehow } }
the problem flash never stops. should stop when elapsedtime reaches 1.5f, specified, because incrementing small amounts (in time.deltatime) doesn't reach 1.5f long time. there i've left out should make reach 1.5f faster, or there number can pick can use accurately act stopping point? numbers elapsedtime tend be: 0, 0.02, 0.03693, 0.054132, etc. small, nothing can pick here.
the following code may looking for. object should flash total amount of time
changing color after intervaltime
using unityengine; using system.collections; public class flashingobject : monobehaviour { private material mat; private color[] colors = {color.yellow, color.red}; public void awake() { mat = getcomponent<meshrenderer>().material; } // use initialization void start () { startcoroutine(flash(5f, 0.05f)); } ienumerator flash(float time, float intervaltime) { float elapsedtime = 0f; int index = 0; while(elapsedtime < time ) { mat.color = colors[index % 2]; elapsedtime += time.deltatime; index++; yield return new waitforseconds(intervaltime); } } }
Comments
Post a Comment