To whom it may concern,
I am trying to create a fading bullet trail (from a raycast) using a LineRenderer with an assigned gradient (Black--> White--> Black) texture. I can make the bullet trail look very pretty in the scene using the "Particles/Additive (Soft)" shader; however, I cannot make the bullet disappear slowly.
My question is:
How can I get a bullet to fade away?
I know that I should have something like this in a script attached to each bullet:
var visibility : float = 1;
visibility -= 2*Time.deltaTime;
if(visibility < 0) this.destroy();
I just have no idea of where to plug in the visibility variable.
Answer by Matt Downey
Oct 24, 2011 at 10:00 PM
In order to create fading bullet tracers, make a bullet prefab with the attached script at the bottom. The BulletTrail Prefab should have a lineRenderer component with this attached material:
Use the shader called "Particles/Additive" when attaching the material to the lineRenderer.
//Script called "SelfDestruct.js" attached to the BulletTrail prefab.
var visibility : float;
var Line : LineRenderer;
function Start ()
visibility = .5; //setting it to 1 doesn't fade until .5
Line = transform.GetComponentInChildren(LineRenderer);
function Update ()
visibility -= Time.deltaTime;
if(visibility < 0) Destroy(gameObject);
Answer by syclamoth
Oct 24, 2011 at 07:32 AM
lineRenderer.material.color = Color.Lerp(Color.clear, Color.white, visibility);
where visibility is a number between 0 and 1. If your material uses a tint colour, save that in Awake, and then use that instead of Color.white.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
3 People are following this question.
How can I can I cast a ray from a gameobject?
Predicting ball direction on collision
Instantiate object and make it follow raycast
Ray curve for bullet gravity effect, or any way to make it