Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by jimm84 · Nov 15, 2019 at 03:29 PM · collisionplayeralphaui imagefadeout

Pain Screen Flash UI and fades

Hello, I was hoping somebody may be able to help or give some pointers on where I have may be going wrong. I have created a PNG (blood) and added a layer to my UI, it is an image.

When the player is hurt, I want the screen to flash up and disappear after a few seconds. I have tried several approaches and all have failed and I'm quite stuck as to how to go about it now.

// // If you read lines " //// PNG FLASHES HERE and vanishes after a couple of seconds /// public GameObject painUI; /// painUI alpho starts at 1 then fades 3/4 or the way down you will see what I am trying to achieve. Thank you for your help.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI; // references the unity UI so you can tinker with it 
 
 public class PlayerStats : MonoBehaviour {
 
     public GameObject ButtonRestart;
     public float speed;    
     public GameObject painUI; // this is the start of the pain screen. 
 
 
     [Header("Sprite Settings:")]
     public Sprite lowHealth; // Displaying sprite for low health.
     public Sprite mediumHealth; // Displaying sprite for medium health.
     public Sprite notbadHealth;  // Displaying sprite for near full health.
     public Sprite fullHealth; // Displaying sprite for full health.
 
     [Space]
 
     [Header("Current Health:")]
     public float health = 100f; // This represent the current health.
 
     private SpriteRenderer spriteRenderer;
 
 
     int damage=25; // Debug visual of health shown
     //public Text GameOverText;//Store a reference to the UI Text component which will display the 'You win' message.
 
 
     // Update is called once per frame
     private void Start () {
         print (health);
         health = 100;
         spriteRenderer = gameObject.GetComponent<SpriteRenderer>(); 
 
     }
         
     /// <summary>
     /// painUI alpho starts at 0 - 
     /// </summary>
 
     private void FixedUpdate()
     {
         playerHealth();
     }
 
 
     /// <summary>
     /// Player health visual increments
     /// </summary>
     public void playerHealth ()
     {
         if (health < 10f)
         {
             spriteRenderer.sprite = lowHealth;
         }
         else if (health < 50f)
         {
             spriteRenderer.sprite = mediumHealth;
         }
         else if (health < 70f)
         {
             spriteRenderer.sprite = notbadHealth;
         }
         else
         {
             spriteRenderer.sprite = fullHealth;
         }
     }
 
     // Contact pain --- when enemy touches player, it causes pain. have tag of "Enemy" on enemy
     void OnCollisionEnter2D(Collision2D _collision){
         if(_collision.gameObject.tag=="Enemy"){
             health-=damage;
             print ("Germ hurts! Ouch!" + health );
 
             //// PNG FLASHES HERE and vanishes after a couple of seconds
             /// public GameObject painUI;
             /// painUI alpho starts at 1 then fades
 
         } 
         // if end
         if (health < 0) {
             print ("player dies");
             Dies ();
         }// second if end
     }
 
     public void Dies() {
         ButtonRestart.SetActive (true);
         speed = 0;
         gameObject.transform.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
         // spawn Gibs
         // Disable player keyboard moves
     }
 
     void OnTriggerEnter2D(Collider2D other) 
     {
         //Check the provided Collider2D parameter other to see if it is tagged "PickUp", if it is...
         if (other.gameObject.CompareTag ("healthPickup")) 
         {
             //... then set the other object we just collided with to inactive.
             other.gameObject.SetActive(false);
 
             //Add one to the current value of our count variable.
             health  = health  + 10;
             print ("Juicy 1 - cool!");
 
             //Update the currently displayed count by calling the SetCountText function.
 
         }
 
 
     }
         
 }
 



Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by lgarczyn · Nov 16, 2019 at 05:26 AM

Simply have a float called "splatterVisibility" that is set to 1f every time the player is hurt, and is multiplied every frame by a number from 0.9 to 0.999 depending on the fading speed you want. Every frame set the alpha of your splatter to that value.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jimm84 · Nov 19, 2019 at 01:18 PM 0
Share

Hello @ceandros , thank you for your help and pardon my ignorance as I'm pretty new to C sharp. So if I'm to associate that float with what you see, the painUI which is a PNG - I have created. How do you link them to each other? A bug flashes up from Unity and saying it couldn't be an "int" and also when I change the Splatter to a float I can't drag the visible game object onto the Public field from inside the editor. Does what you mention above also need to be associated with true or false boolean? Thanks,

     public GameObject ButtonRestart;
     public float speed;    
     //public float SplatterVis = 1;
     public GameObject painUI; // this is the start of the pain screen. 


Thanks,

avatar image lgarczyn jimm84 · Nov 20, 2019 at 03:17 AM 0
Share

".visible" is a boolean, not a float, it cannot be set to anything else than true or false.

What you want is to set the ".color" value to white, but with the alpha set to splattervis.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

194 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Collision between UI element and GameObject 1 Answer

2D Platformer - Picking Up Items & Storing Them C# 0 Answers

Why isn't my health script working ? 3 Answers

Trouble sending message... 2 Answers

shader change alpha around collision 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges