Question by
coolbird22 · Dec 31, 2015 at 05:59 PM ·
raycastlinerendererlaser
How to add a fading laser effect to my laser gun ? Unable to fade out the currently used line renderer over time.
I'm using raycasts to fire rays from my muzzle, and using a line renderer to show the raycasted ray, however I want it to fade out and become invisible over a period of 0.5 seconds. I'm using a Particle/Additive shader and hence need to access the Tint Color in this manner.
Color tempLaserAlpha = gunLine.material.GetColor("_TintColor");
tempLaserAlpha.a = Mathf.Lerp(0.5f, 0.1f, 0.002f);
gunLine.material.SetColor("_TintColor", tempLaserAlpha);
Comment
Best Answer
Answer by coolbird22 · Dec 31, 2015 at 06:28 PM
Managed to get it to work using Coroutines.
if(Physics.Raycast (shootRay, out shootHit, range))
{
StartCoroutine(FadeTo(0.05f, 0.15f));
}
IEnumerator FadeTo(float aValue, float aTime)
{
Color tempLaserAlpha = gunLine.material.GetColor("_TintColor");
tempLaserAlpha.a = 0.5f;
gunLine.material.SetColor("_TintColor", tempLaserAlpha);
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime / aTime)
{
tempLaserAlpha.a = Mathf.Lerp(tempLaserAlpha.a,aValue,t);
gunLine.material.SetColor("_TintColor", tempLaserAlpha);
yield return null;
}
}
Your answer
Follow this Question
Related Questions
Raycast Getting Caught 0 Answers
Unity 2D: how to fire a renderline with raycast upward? 0 Answers
How do I destroy particles with raycast? 0 Answers