1

I'm creating a 2D top-down shooting game. I want to add a damage effect to the enemy when the bullet collides with it.

Is there a way to turn the collided sprite white for 0.5 seconds and then fade out to the normal sprite.

void OnCollisionEnter2D(Collision2D collision)
{
    if(collision.gameObject.tag == "Enemy")
    {
        EnemyController enemy = collision.transform.GetComponent<EnemyController>();
        if(enemy != null)
        {
            // Take the actual damage
            enemy.TakeDamage(damage);

            // Change sprite color temporarily here
        }
    }
}
KyryloRen
  • 41
  • 1
  • 4
  • well, you can tint it by changing the colour – BugFinder Jan 28 '20 at 23:35
  • you could change the color then start a coroutine that waits a particular amount of time then changes it back – Jonathan Alfaro Jan 29 '20 at 01:32
  • @Darkonekt how would you change the sprite back if you change the color using `enemySpriteRenderer.color = Color.white;`? – KyryloRen Jan 29 '20 at 02:50
  • you can create a coroutine and inside the coroutine use WaitForSeconds and change the color again.... Look for Unity Coroutines on google. – Jonathan Alfaro Jan 29 '20 at 03:30
  • Does this answer your question: [How make the script wait/sleep in a simple way in unity](https://stackoverflow.com/questions/30056471/how-make-the-script-wait-sleep-in-a-simple-way-in-unity) ? – derHugo Jan 29 '20 at 06:22
  • By the way, if the enemy can take multiple hits, then there may be a need for more logic than just change color, wait, revert. You would need a coroutine to revert but the values in it would be updated if a new collision occurs. – Everts Jan 29 '20 at 08:12

2 Answers2

4

I would do it with a coroutine:

public void TakeDamage()
{
    // Tints the sprite red and fades back to the origin color after a delay of 1 second
    StartCoroutine(DamageEffectSequence(sr, Color.red, 2, 1));
}

IEnumerator DamageEffectSequence(SpriteRenderer sr, Color dmgColor, float duration, float delay)
{
    // save origin color
    Color originColor = sr.color;

    // tint the sprite with damage color
    sr.color = dmgColor;

    // you can delay the animation
    yield return new WaitForSeconds(delay);

    // lerp animation with given duration in seconds
    for (float t = 0; t < 1.0f; t += Time.deltaTime/duration)
    {
        sr.color = Color.Lerp(dmgColor, originColor , t);

        yield return null;
    }

    // restore origin color
    sr.color = originColor;
}
sight
  • 355
  • 1
  • 2
  • 11
-3

There are two simple methods to add a time delay to your function

1.Invokes :

In your collision, change the color of sprite by getting the sprite component by using

Getcomponent<sprite>().color = color.white;
Invoke("revertcolor" , 0.5f); (it will add a time delay and then call the other function)

then create another function like revertcolor() and revert the color in this function by using Getcomponent().color = originalcolor;

2.Coroutines : Another simple method is using coroutines..... just create a coroutine using

ienumerator revertcolor()
{
using Getcomponent<sprite>().color = originalcolor;
yield return new waitforseconds(0.5f);  // It will add a time delay in your function
}
> After creating this coroutine, just call it in you collision by using startcoroutine(revertcolor);
using Getcomponent<sprite>().color = color.white;
halfer
  • 19,824
  • 17
  • 99
  • 186
  • What are those `using` statements for? I'ld guess you copy pasted this wrong ;) please also fix your typos! It's `IEnumerator`, `WaitForSeconds` and `sprite` would probably be `Sprite` but `Sprite` is no component ... In general the convention for Method names is PascalCase (starting with a capital letter) – derHugo Jan 29 '20 at 06:16
  • Well intellisense don't work on stack overflow so there can be a lot of typos Secondly i was talking about the sprite component coz you can change the color of a sprite by means of a component... One more thing the word "using" is not a keyword come on, i wrote using just to point out the method – Arslan Mughal Jan 29 '20 at 06:38
  • 1
    1. That I why friendly I told you there were typos so you can fix them. I type most of my answers on the phone. You don't need Intellisense in order to write correct code. 2. There is no component called `Sprite` I would know of - there is only [`Sprite`](https://docs.unity3d.com/ScriptReference/Sprite.html) which as said is no component. 3. [`using`](https://learn.microsoft.com/dotnet/csharp/language-reference/keywords/using-statement) **is** a keyword in c# and therefore you should be careful to not have it in your code examples by accident. – derHugo Jan 29 '20 at 06:52
  • well thats so nice of you and let me correct my self, i wanted to say sprite renderer instead of sprite, but typed the answer so casually thinking the people might understand what i wanted to say, anyways happy coding again – Arslan Mughal Jan 29 '20 at 07:01
  • If you just want to provide some tips on how to, then a comment is more appropriate. An answer is expected to provide accurate help, yours is actually misleading by mixing text containing keywords, typos and approximate hints that the reader will have to figure out. If people knew what to do, they would not be here asking. Also, you are invoking a coroutine, I am not sure this works, but the Invoke delays by 0.5s while the coroutine would also wait for 0.5s. This is again misleading. – Everts Jan 29 '20 at 08:08
  • @everts Thank you very much, i'll try using the comment section next time... secondly i'm not invoking a co-routine.... i'm just identifying two scenarios. obviously invoking a co-routine is ridiculous. – Arslan Mughal Jan 29 '20 at 08:16
  • I reported this answer to the Review Queue, and unfortunately it was seen by a moderator rather than the queue. The flag was declined, but I think it might have been accepted had a reviewer seen it instead. No matter - my advice nevertheless is if readers ask for edits/improvements to an answer, please make them. The purpose of Stack Overflow is to curate high-quality questions and answers that are helpful for future readers. – halfer Jan 30 '20 at 22:00