- Home /
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.
}
}
}
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.
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,
".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
Follow this Question
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