- Home /
How do I only target the alpha of my SpriteRenderers?
Hi there,
Simple question I know, but for some reason I can't get it working. I'm trying to apply a fade to the alpha channel on ALL children of a certain object, if they have a correct tag.
Here's a gif showing my current situation:
And here's the full script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SeeThroughObjects : MonoBehaviour {
private Color startColor;
private Color transparentColor = new Color(255, 255, 255, 0);
public bool isFadingOut;
public bool isFadingIn;
void Start()
{
foreach (SpriteRenderer sr in GetComponentsInChildren<SpriteRenderer>())
{
startColor = sr.material.color;
}
}
private void Update()
{
if (isFadingIn)
StartCoroutine(FadeIn());
if (isFadingOut)
StartCoroutine(FadeOut());
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.tag == "Player")
{
isFadingOut = true;
isFadingIn = false;
}
}
void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Player")
{
isFadingOut = false;
isFadingIn = true;
}
}
IEnumerator FadeIn()
{
foreach (SpriteRenderer sr in GetComponentsInChildren<SpriteRenderer>())
{
if (sr.CompareTag("HiddenSprite")) //If It's an object tagged with HiddenSprite, leave as is.
{
sr.material.color = startColor;
}
else //Make all children that are NOT tagged with Hiddensprite transparent.
{
Debug.Log("Fading IN!");
sr.material.color += new Color(0, 0, 0, Time.deltaTime*2);
yield return new WaitForSeconds(0.0001f);
}
}
}
IEnumerator FadeOut()
{
foreach (SpriteRenderer sr in GetComponentsInChildren<SpriteRenderer>())
{
if (sr.CompareTag("HiddenSprite")) //If It's an object tagged with HiddenSprite, leave as is.
{
sr.material.color = startColor;
}
else //Make all children that are NOT tagged with Hiddensprite visible again.
{
Debug.Log("Fading OUT!");
sr.material.color -= new Color(0, 0, 0, Time.deltaTime*2);
yield return new WaitForSeconds(0.0001f);
}
}
}
}
First of all, I don't have a way of controlling the speed. It even looks like each object is doing the fadings at their own pace, not sure though.
Secondly, when the sprites are fading back in, they turn white instead of their original color.
I would have thought I just needed to change this bit:
Debug.Log("Fading IN!");
sr.material.color += new Color(0, 0, 0, Time.deltaTime*2);
yield return new WaitForSeconds(0.0001f);
To something like:
Debug.Log("Fading IN!");
sr.material.color += new Color(255, 255, 255, Time.deltaTime*2);
yield return new WaitForSeconds(0.0001f);
But that just makes things explode even more.
Any help would be greatly appreciated!
Thanks.
Answer by AaronBacon · Oct 25, 2017 at 09:29 AM
I know one way to do this would be a CanvasGroup, since your effect seems purely visual. If you put the objects as children to the CanvasGroup, you can use code to simply change the Alpha value of the group of objects.
Eg.
public CanvasGroup buildings;
void Update()
{
if (fadeIn == true)
{
buildings.Alpha == buildings.Alpha + Time.deltaTime * 2;
if (buildings.Alpha > 1)
{
fadeIn = false;
}
}
}
Right, now I have something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SeeThrough : $$anonymous$$onoBehaviour {
private CanvasGroup thisCanvasGroup;
private bool isFadingOut = false;
private bool isFadingIn = false;
private float $$anonymous$$Val = 0f;
private float maxVal = 1f;
static float t = 0f;
void Start ()
{
thisCanvasGroup = GetComponent<CanvasGroup>();
}
void Update ()
{
if (isFadingOut)
FadeOut();
else if (isFadingIn)
FadeIn();
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
isFadingOut = true;
isFadingIn = false;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.tag == "Player")
{
isFadingOut = false;
isFadingIn = true;
}
}
public void FadeOut()
{
Debug.Log("Remove alpha please!");
thisCanvasGroup.alpha = $$anonymous$$athf.Lerp($$anonymous$$Val, maxVal, t*Time.deltaTime);
}
public void FadeIn()
{
Debug.Log("Add alpha please!");
thisCanvasGroup.alpha = $$anonymous$$athf.Lerp(maxVal, $$anonymous$$Val, t*Time.deltaTime);
}
}
I don't understand the $$anonymous$$athf.Lerp. It's supposed to slowly count from 0 to 1 and the other way around, right?
$$anonymous$$athf.Lerp is work like this: Assume you have result = $$anonymous$$athf.Lerp(a,b,t)
, result will be the same as ((b-a) * t) + a
. Note t must be a value between 0 and 1. For anything greater than 1, I think it will be taken as 1.
For your issue here, you might want to control your fade in/out speed using a fixed time like this:
timer = 0f;
fixedTime = 3f; //could be any positive value in sec
void Update(){
if(timer < fixedTime){
timer += Time.deltaTime();
}
result = $$anonymous$$athf.Lerp(a,b,timer/fixedTime);
}
I've noticed you are using Time.deltaTime here, but yield it for 0.0001f second.
sr.material.color -= new Color(0, 0, 0, Time.deltaTime*2);
yield return new WaitForSeconds(0.0001f);
I'll suggest you use yield return null;
ins$$anonymous$$d.This will yield your coroutine for exactly one update frame.
Thank you, ended up solving it through animations. Still can't get the matf.lerp or coroutines to work.
Your answer
![](https://koobas.hobune.stream/wayback/20220612141811im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I make sprite renderer's sprite change in runtime c#? 0 Answers
Crop/Mask a Sprite on runtime 0 Answers
Why is my sprite not the original resolution? 2 Answers
I have an animaiton problem about sprite loading. 0 Answers
Unity2D Sprite Fading Issues 1 Answer