- Home /
How to fade in a GameObject with alpha ?
Hi guys ! I found this script to fade in a game Object, it work but i want that the alpha stop at 140, now in this moment it go at 255... there is a way to stop the Fade at 140 ? Thank you :)
public class FadeAlpha : MonoBehaviour {
[SerializeField] private float fadePerSecond = 2.5f;
private void Update() {
var material = GetComponent<Renderer>().material;
var color = material.color;
material.color = new Color(color.r, color.g, color.b, color.a + (fadePerSecond * Time.deltaTime));
}
}
Answer by UnityCoach · May 14, 2017 at 07:41 PM
You can use an AnimationCurve to do this.
public class FadeAlpha : MonoBehaviour
{
[SerializeField] private AnimationCurve fadeCurve;
Renderer _renderer;
Color _color;
float _timer = 0f;
private void Awake ()
{
_renderer = GetComponent<Renderer>(); // do this in awake, it has an impact on performances in Update
_color = _renderer.material.color;
}
private void Update()
{
_timer += Time.deltaTime;
_color.a = fadeCurve.Evaluate (_timer);
_renderer.material.color = _color;
}
}
Answer by Fritsl · May 14, 2017 at 08:02 PM
You might instead want to look into using tween libraries - http://dotween.demigiant.com/ as an example ;)
Your answer
Follow this Question
Related Questions
How can I change texture alpha with another texture by script? 0 Answers
How to add alpha cutout shader to this curveworld shader 1 Answer
Help me finish my room generator 0 Answers
How to move the object to where the object is already pointing to? 1 Answer
I have problem with collision :D,Im New at Unity and Trying To make ,,Game" With Youtube Toturial... 1 Answer