Search Unity

Change alpha value to transparent when colliding (Lerp, DOTween)

Discussion in '2D' started by Big_F99, Jan 29, 2022.

  1. Big_F99

    Big_F99

    Joined:
    Jun 21, 2021
    Posts:
    2
    Hey guys!
    I have a little game where my player gets hit by an enemy and after each hit I want to change his alpha value a little bit until he almost becomes transparent (Color lerp). I saw that it is possible by doing a color lerp or with the DOTween plugin. Sadly I didn't manage to do it. That is what I have now:
    Code (CSharp):
    1. private void Fade(float endValue, float fadeDuration, TweenCallback onEnd)
    2.         {
    3.             if(fadeTween != null)
    4.             {
    5.                 fadeTween.Kill(false);
    6.             }
    7.  
    8.             fadeTween = canvasGroup.DOFade(endValue, fadeDuration);
    9.             fadeTween.onComplete += onEnd;
    10.         }
    11.  
    12.         private void FadeOut(float fadeDuration)
    13.         {
    14.             Fade(0f, fadeDuration, () =>
    15.             {
    16.                 canvasGroup.interactable = false;
    17.                 canvasGroup.blocksRaycasts = false;
    18.             });
    19.         }
    Nothing happens to my player. Is something wrong in my code or is there another solution? I would be glad if someone can help me out :)