- Home /
How to disable trail renderer
I add trail renderer for my bullet. I am also using object pooling. When I reuse by bullet the trail renderer draw all over the screen.
I tried enable and disable but it does not work.
Is there a way to disable trail renderer?
so, did you fix your problem? got the same atm, trailrenderer does not seem to react to scripts
Answer by Junglej0hn · Dec 07, 2017 at 01:26 PM
To anyone who still has this problem in 2017. Use myTrailRenderer.Clear()
Thank you so much! I was having issues with this trying to disabled then re-enable the renderer.
Thanks very much
Answer by sethuraj · Oct 24, 2013 at 12:13 PM
When you reuse the bullet set
gameObject.GetComponent<TrailRenderer>().enabled=false;
to disable the trail render.And on firing the bullet, set it to true.
it will work.make sure you are calling this in a script which is attached to a gameobject that has Trail Render.If still its not working,please show your code so that we can diagnose properly.
Answer by wijesijp · Oct 24, 2013 at 04:43 PM
The code I have so far. I wrote it by checking out someof the posts in the site. It look OK most of the time. But sometimes it does not work
private TrailRenderer line;
private float _trailTimer = 0.1f;
private bool alive = true;
private Vector3 movingDirection;
void Awake()
{
line = gameObject.GetComponent<TrailRenderer>();
line.time = -1;
}
// Use this for initialization
public virtual void Start()
{
gameObject.renderer.enabled = false;
trans = gameObject.transform;
spentLife = lifeTime;
}
public virtual void Init(CreepAI target, Vector3 startPosition, float damageAmount, float areaOfEffect, string damageCauser)
{
this.damageAmount = damageAmount;
this.areaOfEffect = areaOfEffect;
this.damageCauser = damageCauser;
movingDirection = Vector3.Normalize(target.gameObject.transform.position - gameObject.transform.position);
}
// Update is called once per frame
protected virtual void FixedUpdate()
{
if (line.time <= 0.0f)
{
_trailTimer -= Time.fixedDeltaTime;
if (_trailTimer <= 0)
{
gameObject.renderer.enabled = true;
line.time = 0.50f;
_trailTimer = 0.1f;
}
}
trans.position += movingDirection * Time.fixedDeltaTime * speed;
spentLife -= Time.fixedDeltaTime;
if (spentLife <= 0.0f)
{
line.time = -1;
ObjectPoolManager.DestroyPooled(gameObject);
}
}
void OnTriggerEnter(Collider other)
{
line.time = -1;
DestroyProjectile();
}
public virtual void DestroyProjectile()
{
gameObject.renderer.enabled = false;
ObjectPoolManager.DestroyPooled(gameObject);
}
Answer by QuintenVTW · Aug 17, 2017 at 11:24 AM
I've got a similar issue.. I'm making a game with a rolling ball for android. And in this game is a teleporter, but every time if the ball (the ball has a trail) passes trough the teleporter you can see the trail going from teleport pad A to pad B .. very annoying. I tried to stop it but it did not work out.
public void OnTriggerEnter(Collider other) { other.GetComponent<TrailRenderer>().enabled = false; other.transform.position = Destination.transform.position; other.transform.rotation = Destination.transform.rotation; other.GetComponent<TrailRenderer>().enabled = true; }
Answer by Humman · Feb 18, 2018 at 01:20 AM
Its simple, put Trail render component in empty node under object with trail and use gameObject.setActive() for this child node
that doesnt work since the trail is still there after you setActive(True)
Your answer
