Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question how do i get this animation to NOT play on awake?

Discussion in 'Scripting' started by SpyderManToo, Mar 3, 2021.

  1. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    so in my script, i have it as if the health is above 40%, hide the low health vignette by playing an animation but the thing is, when you start out the game, you have max health so it plays the animation for no reason, how do i make it so it only plays if the vignette was shown previously?
    heres the script:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class PlayerStats : MonoBehaviour
    5. {
    6.     [Header("Health")]
    7.     public int maxHealth;
    8.     private int currentHealth;
    9.  
    10.     [Header("References")]
    11.     public Rigidbody2D playerRb;
    12.     public float enemyContactKb;
    13.     public Animator lowHealthVignette;
    14.  
    15.     Enemy enemy;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         currentHealth = maxHealth;
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         if (currentHealth <= 0)
    27.         {
    28.             Debug.Log("You ded");
    29.             Time.timeScale = 0f;
    30.         }
    31.  
    32.         if(currentHealth <= currentHealth * 0.4)
    33.         {
    34.             lowHealthVignette.SetBool("LowHealth", true); //Turn on vignette
    35.         }
    36.         else
    37.         {
    38.             lowHealthVignette.SetBool("LowHealth", false); //Turn off vignette
    39.         }
    40.     }
    41.  
    42.     public void TakeDamage(int damageToTake)
    43.     {
    44.         currentHealth -= damageToTake;
    45.     }
    46.  
    47.     private void OnCollisionEnter2D(Collision2D collision)
    48.     {
    49.         if (collision.gameObject.CompareTag("Enemy"))
    50.         {
    51.             Debug.Log("You came into contact with an enemy");
    52.             enemy = collision.gameObject.GetComponent<Enemy>();
    53.             currentHealth -= enemy.contactDamage;
    54.         }
    55.     }
    56. }
    57.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    This line makes no sense:
    Code (csharp):
    1. if(currentHealth <= currentHealth * 0.4)
    But I don't think it is the cause of the problem.
     
  3. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    essentially it means, if you only have 40% or less of your original health, do something. so if u had 100 max health, its saying if you have 0-40 health, do the thing
     
  4. tylerlazarus13

    tylerlazarus13

    Joined:
    Mar 3, 2021
    Posts:
    1
    I think you mean to check the current health against the max health. Also with your effect problem, make sure the effect isn't being play at the start for any reason. Check the object making the effect and the animator to make sure.
     
    Joe-Censored likes this.
  5. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    That's not what it means, because maxHealth is nowhere on that line. I believe you meant to put:
    Code (csharp):
    1. if(currentHealth <= (maxHealth * 0.4))
     
  6. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    wops, yup ur rigiht
    also, i kinda just abandoned the whole idea of animations becuase i hate doin animations and its way too confusing for me. how can i make the alpha of an image fade in through code?
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    What kind of image? If you're talking about UI, I add a CanvasGroup component and then adjust its alpha value over time.
    https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/class-CanvasGroup.html

    Simple example off the top of my head:
    Code (csharp):
    1. public float FadeInTime = 2f;
    2. private float fadeTimeSoFar = 0f;
    3. public CanvasGroup CanvasGroupComponent;
    4.  
    5. void Update()
    6. {
    7.     if (fadeTimeSoFar < FadeInTime)
    8.     {
    9.         fadeTimeSoFar += Time.deltaTime;
    10.         float percent = fadeTimeSoFar / FadeInTime;
    11.         if (percent > 1f)
    12.         {
    13.             percent = 1f;
    14.         }
    15.         CanvasGroupComponent.Alpha = percent;
    16.     }
    17. }
     
  8. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    i added the code, now for some stupid reason, when the game starts, the vignette, instead of staying at transparent, fades in, why does it do this?

    heres the code
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerStats : MonoBehaviour
    4. {
    5.     [Header("Health")]
    6.     public int maxHealth;
    7.     private int currentHealth;
    8.  
    9.     [Header("Effects")]
    10.     public float FadeInTime;
    11.     private float fadeTimeSoFar = 0f;
    12.     public float FadeOutTime;
    13.     private float fadeOutTimeSoFar = 0f;
    14.     public CanvasGroup CanvasGroupComponent;
    15.  
    16.     [Header("References")]
    17.     public Rigidbody2D playerRb;
    18.  
    19.     Enemy enemy;
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         currentHealth = maxHealth;
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         if (currentHealth <= 0)
    31.         {
    32.             Debug.Log("You ded");
    33.             Time.timeScale = 0f;
    34.         }
    35.  
    36.         if (currentHealth <= maxHealth * 0.4) //Check if you have <= 40% health
    37.         {
    38.             if (fadeTimeSoFar < FadeInTime) //If the vignette isn't fully faded in yet:
    39.             {
    40.                 fadeTimeSoFar += Time.deltaTime; //Add time to the fade in time
    41.                 float percent = fadeTimeSoFar / FadeInTime; //Calculate what percentage it is at.
    42.                 if (percent > 1f) //If the percent is more than 1
    43.                 {
    44.                     percent = 1f; //Set the percent to 1 to avoid errors
    45.                 }
    46.                 CanvasGroupComponent.alpha = percent; //Set the alpha to the current percentage so it can fade in
    47.             }
    48.         }
    49.         else
    50.         {
    51.             if (fadeOutTimeSoFar < FadeOutTime) //If the vignette isn't fully faded out yet:
    52.             {
    53.                 fadeOutTimeSoFar += Time.deltaTime; //Add time to the fade out time
    54.                 float percent = fadeOutTimeSoFar / FadeOutTime; //Calculate what percentage it is at.
    55.                 if (percent < 0f) //If the percent is less than 0:
    56.                 {
    57.                     percent = 0f; //Set the percent to 0 to avoid errors
    58.                 }
    59.                 CanvasGroupComponent.alpha = percent; //Set the alpha to the current percentage so it can fade out
    60.             }
    61.         }
    62.     }
    63.  
    64.     public void TakeDamage(int damageToTake)
    65.     {
    66.         currentHealth -= damageToTake;
    67.     }
    68.  
    69.     private void OnCollisionEnter2D(Collision2D collision)
    70.     {
    71.         if (collision.gameObject.CompareTag("Enemy"))
    72.         {
    73.             Debug.Log("You came into contact with an enemy");
    74.             enemy = collision.gameObject.GetComponent<Enemy>();
    75.             currentHealth -= enemy.contactDamage;
    76.         }
    77.     }
    78. }
    79.  
     
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The problem is you tried to rewrite the code to fade out instead of fade in, starting with line 51, but I didn't write it to do that, since I wasn't aware you were referring to the same vignette in your question about fading "an image". Line 53 is a problem, because you are adding instead of subtracting. Adding will increase the alpha of the image, instead of decrease.
     
  10. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    ok so i changed line 53 to -= time.deltatime
    and i changed line 13 to = 1f; did i do this part right?

    how do i make the code fade out?
     
  11. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    @Joe-Censored how do i rewrite it corerctly to make it fade out?
     
  12. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Just Debug.Log the values there. The goal is to get "percent" go from 1 to 0 over time. I don't have a C# compiler on this computer I'm on the forums with, so can't run it to look at what it is doing. It probably will be obvious what is currently going on, and what to change when you can see what is currently happening.
     
  13. SpyderManToo

    SpyderManToo

    Joined:
    Dec 8, 2020
    Posts:
    387
    Code (CSharp):
    1. if (currentHealth <= maxHealth * 0.4) //Check if you have <= 40% health
    2.         {
    3.         else
    4.         {
    5.             if (fadeOutTimeSoFar < FadeOutTime) //If the vignette isn't fully faded out yet:
    6.             {
    7.                 fadeOutTimeSoFar -= Time.deltaTime; //Add time to the fade out time
    8.                 float _percent = fadeOutTimeSoFar / FadeOutTime; //Calculate what percentage it is at.
    9.                 if (_percent < 0f) //If the percent is less than 0:
    10.                 {
    11.                     _percent = 0f; //Set the percent to 0 to avoid errors
    12.                 }
    13.                 CanvasGroupComponent.alpha = _percent; //Set the alpha to the current percentage so it can fade out
    14.                 Debug.Log(_percent);
    15.             }
    16.         }
    i tested this code here, it doesn't even run...help
    heres the full script:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerStats : MonoBehaviour
    4. {
    5.     [Header("Health")]
    6.     public int maxHealth;
    7.     public int currentHealth;
    8.  
    9.     [Header("Effects")]
    10.     public float FadeInTime;
    11.     private float fadeTimeSoFar = 0f;
    12.     public float FadeOutTime;
    13.     private float fadeOutTimeSoFar = 1f;
    14.     public CanvasGroup CanvasGroupComponent;
    15.  
    16.     [Header("References")]
    17.     public Rigidbody2D playerRb;
    18.  
    19.     Enemy enemy;
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         currentHealth = maxHealth;
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         if (currentHealth <= 0)
    31.         {
    32.             Debug.Log("You ded");
    33.             Time.timeScale = 0f;
    34.         }
    35.  
    36.         if (currentHealth <= maxHealth * 0.4) //Check if you have <= 40% health
    37.         {
    38.             if (fadeTimeSoFar < FadeInTime) //If the vignette isn't fully faded in yet:
    39.             {
    40.                 fadeTimeSoFar += Time.deltaTime; //Add time to the fade in time
    41.                 float percent = fadeTimeSoFar / FadeInTime; //Calculate what percentage it is at.
    42.                 if (percent > 1f) //If the percent is more than 1
    43.                 {
    44.                     percent = 1f; //Set the percent to 1 to avoid errors
    45.                 }
    46.                 CanvasGroupComponent.alpha = percent; //Set the alpha to the current percentage so it can fade in
    47.             }
    48.         }
    49.         else
    50.         {
    51.             if (fadeOutTimeSoFar < FadeOutTime) //If the vignette isn't fully faded out yet:
    52.             {
    53.                 fadeOutTimeSoFar -= Time.deltaTime; //Add time to the fade out time
    54.                 float _percent = fadeOutTimeSoFar / FadeOutTime; //Calculate what percentage it is at.
    55.                 if (_percent < 0f) //If the percent is less than 0:
    56.                 {
    57.                     _percent = 0f; //Set the percent to 0 to avoid errors
    58.                 }
    59.                 CanvasGroupComponent.alpha = _percent; //Set the alpha to the current percentage so it can fade out
    60.                 Debug.Log(_percent);
    61.             }
    62.         }
    63.     }
    64.  
    65.     public void TakeDamage(int damageToTake)
    66.     {
    67.         currentHealth -= damageToTake;
    68.     }
    69.  
    70.     private void OnCollisionEnter2D(Collision2D collision)
    71.     {
    72.         if (collision.gameObject.CompareTag("Enemy"))
    73.         {
    74.             Debug.Log("You came into contact with an enemy");
    75.             enemy = collision.gameObject.GetComponent<Enemy>();
    76.             currentHealth -= enemy.contactDamage;
    77.         }
    78.     }
    79. }
    80.