Search Unity

Trigger Animation when Health is Low?

Discussion in '2D' started by RuneShiStorm, Oct 10, 2017.

  1. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    Hi all!
    I followed this HealthBar tutorial for my Enemies in my game.
    It works fine and all but I want to be able to trigger a certain animation once health is under 30 points.
    Make sense? Basically I have a huge enemy type that should kneel once his health is low.
    So instead of triggering "Die" animation I want it to trigger "kneel" so you can hit him a few more times before His health reach 0 and trigger "die"
    I think the function should be called in this piece of code below but not sure :( (still noob at this)
    Hope it make sense and I hope there is a simple solution.

    Code (csharp):
    1.  
    2.  
    3. public override IEnumerator TakeDamage()
    4.     {
    5.         if (!healthCanvas.isActiveAndEnabled)
    6.         {
    7.             healthCanvas.enabled = true;
    8.         }
    9.         healthStat.CurrentValue -= 10;
    10.  
    11.         if (!IsDead)
    12.         {
    13.             MyAnimator.SetTrigger("damage");
    14.         }
    15.         else
    16.        
    17. // I THINK THE FUNCTIONS SHOULD BE CALLED HERE BEFORE HE DIES
    18.         {
    19.             MyAnimator.SetTrigger("die");
    20.             yield return null;
    21.         }
    22.  
    Thanks in advance!
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You're in the right place. Just read through the logic, step by step. It currently says:
    1. Reduce his health value by 10 (line 9).
    2. Is he dead? If he's not dead (line 11), then show the damage animation (line 13).
    3. Otherwise (if he is dead), show the die animation (line 19).
    All of this is made more confusing by the pointless presentation of this as a Coroutine, but still, that's all that is going on.

    Now, compare this logic above to what you want to happen. Can you write out the desired logic, in plain English, step-by-step as above?

    Once you've done that, I think you'll probably see how to convert that logic into code!
     
  3. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    yes, But I'm not sure HOW this code understand when Enemy health is 30. Since i can set the health in the inspector.
    By that logic I would say:
    1. If health is 30 or less and Not dead don't show damage animation. Instead, show "kneel" animation.

    I tried something like:
    Code (csharp):
    1.  
    2. else if (!IsDead && healthStat.CurrentValue = 30);
    3.         {
    4.             MyAnimator.SetTrigger("damage") = false;
    5.             MyAnimator.SetTrigger("kneel");
    6.         }
    7.  
    But its all error.. Its like.. I can read and understand Codes, but I cant write them just yet... >_<
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I think the main problem in that code is that you've used = instead of ==.

    In C# (and other C-derived languages), = is used for assigning a new value to a variable, whereas == is used for comparing two values for equality.

    Of course you should probably use <= (less than or equal) here anyway.
     
  5. hlw

    hlw

    Joined:
    Aug 12, 2017
    Posts:
    250
    Also:
    • (if) You check if he's not dead and trigger damage anim
    • (else if) Then if he's not dead, you check if it has less than 30 points and trigger kneel anim
    • (else) And if he's dead, you trigger death anim
    Problem is, if he's not dead, as you check for less than 30 hp in an else if, this won't work.
    Monster won't be dead, so it will trigger the damage anim. And wont do anything else.

    in the first statement, you should check if he is not dead, but also if he has more than 30 hp:

    Code (CSharp):
    1. if (!IsDead && healthStat.CurrentValue > 30))
    2.         {
    3.             MyAnimator.SetTrigger("damage");
    4.         }
     
    Last edited: Oct 16, 2017
    RuneShiStorm likes this.
  6. MurcsRoyce

    MurcsRoyce

    Joined:
    Jul 19, 2017
    Posts:
    38
    I would like to add to this. You need to make sure your animations are set up correctly for transitions. If they are your code will work more. Having code typed right and animations set incorectly, is going to produce the same undesired results. Your animations should have bool true or false set in the inspector and there transitions set correctly. You need to start over from scratch and start with animations. If you dont you possibly might keep strugling .
     
    RuneShiStorm likes this.
  7. RuneShiStorm

    RuneShiStorm

    Joined:
    Apr 28, 2017
    Posts:
    264
    EDIT:
    I frogot to make an update.. I solved it like this:

    Code (csharp):
    1.  
    2.  
    3. if (!IsDead)
    4.         {
    5.             MyAnimator.SetTrigger("damage");
    6.  
    7.             if (healthStat.CurrentValue > 30)
    8.             {
    9.                 MyAnimator.SetTrigger("damage");
    10.             }
    11.             else
    12.             {
    13.                 MyAnimator.SetTrigger("kneel");
    14.             }
    15.         }
    16.  
    17.  
    18.  
    thanks for the help guys!
     
    JoeStrout likes this.