- Home /
Make a 2D sprite flash?
I'm trying to get my player to flash invisible and visible while it's invulnerable after being hit. Presently, the player is hit and becomes invulnerable for a short time as intended, however it just becomes invisible for as long as it is invulnerable. I wanted to know if there was a way to make a bool, in this instance an "IsVisible" bool, that would toggle on and off every X amount of seconds while another bool is true. Basically, the "IsVisible" would toggle on and cause the sprite to render, toggle off and cause the sprite to not render, on an interval so that it flashed rendered and not rendered. All help would be appreciated. I'm trying to polish up my game for showcase on Sunday. Thanks.
if (invulnerable)
{
player.GetComponent<SpriteRenderer>().enabled = false;
}
else
{
player.GetComponent<SpriteRenderer>().enabled = true;
}
Are u calling flash () function inside update ? And print (invulneable ) to debug it properly .
Answer by rdougla7 · Nov 17, 2016 at 07:19 AM
@KiethFurry I know this is old, but, this would probably work.
This is what I referenced.
In the unity editor: Have a collider (box2d,circle2d,poly2d,whatver) on your player, and whatever is hitting your player. Set them both as triggers. If you have multiple objects that you want to have different effects on the player, you can use a tag. Otherwise disregard my "if(other.gameobject.tag)" statement. Note that in my experience using this method, if an untagged object hits your player, the game breaks.
Script Explanation: Make a variable for the players spriterenderer, and it's collider. Get those components in your start function so you're actually referencing something. Use "void OnTriggerEnter2D" to look for the collision. Collider2D Other is what is hitting the player. This is where you can look for tags or not. You would then call the "IEnumerator Flash()". To call you have to use "StartCorutine(information)".
Flash() Explanation: Use the loop to determine how many times you want your player to flash. The time between each flash is determined by "x". "x" is passed into the function when it's called as "flashSpeed", which can be set from editor for tweaking.
public float flashSpeed;
PolygonCollider2D polyColl;
SpriteRenderer spRndrer;
void Start() {
polyColl = GetComponent<PolygonCollider2D>();
spRndrer = GetComponent<SpriteRenderer>();
}
void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.tag == "objectThatHitsMeTag")
StartCoroutine(Flash(flashSpeed));
}
IEnumerator Flash(float x) {
polyColl.enabled = false;
for (int i = 0; i < 10; i++) {
spRndrer.enabled = false;
yield return new WaitForSeconds(x);
spRndrer.enabled = true;
yield return new WaitForSeconds(x);
}
polyColl.enabled = true;
}
Answer by Amanna · May 21, 2016 at 06:28 AM
public int delay =100;
public SpriteRenderer mySpriteRenderer;
int counter;
bool toggle=false;
void Update() // you can you FixedUpdate for fixed flash rate
{
Flash(mySpriteRenderer);
}
void Flash(SpriteRenderer spriteRen)
{
if(counter>=delay)
{
counter = 0;
toggle=!toggle;
if(toggle)
{
spriteRen.enabled=true;
}
else
{
spriteRen.enabled=false;
}
}
else
{
counter++;
}
}
Thank you. I'll test that once I have another issue figured out. I didn't change a thing in my game and yet every time I load it up now it has horrible frame rate issues. I tried putting in code to set the targeted frame rate, but this didn't resolve the issue. I greatly appreciate the help with the flashing and I really want to test it, but that's looking kind of tough with the game lagging the way it is. It's a 2D game that's never lagged before, and I even closed it and restarted my laptop.
Also I just tested this code you gave me, and the player did not flash. The player remained completely unchanged visual-wise,
if (invulnerable) { Flash(mySpriteRenderer); }
Actually just fixed the frame rate issue randomly. Not sure what fixed it. But either way the code you gave me hasn't worked, although that could just be me not putting it in the correct place.
Because Flash() func works perfectly , just call it properly .. I don't know the flow of your source code.
Your answer
Follow this Question
Related Questions
Is there a way to change a Sprite's Rect at runtime? 0 Answers
Why can't the camera render the sprite? 2 Answers
Changing to second slice in sprite regardless or current sprite. 0 Answers
Change sprite using sprites in Assets folder through script 0 Answers
Puppet animations by sorting layers, generate overposition in prefab 1 Answer