Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Re-use bullet with TrailRenderer?

Discussion in 'Scripting' started by VipHaLongPro, Apr 23, 2015.

  1. VipHaLongPro

    VipHaLongPro

    Joined:
    Mar 8, 2015
    Posts:
    49
    Using TrailRenderer to create a bullet trail effect is OK but looks like it works only when the bullet is destroyed at the end (when collided with something) using the Destroy() method.

    The problem is raised up when I want to re-use the bullet (don't destroy it but push it back to some pool and pop it out later for reusing). The TrailRenderer seems unable to be stopped from tracking the bullet's position and rendering a messy trail while the bullet is inactive. So the first time the bullet is used, the trail is rendered OK. Next time when the bullet is reused, there is a trail connecting the last position of the bullet (when it collided with some target before) and the barrel tip. That's of course a mess. While it should render normally starting from the bullet's end to the barrel tip.

    I've tried disabling the trail object (having TrailRenderer component) together with the bullet being set inactive and re-enabling the trail object when the bullet is set active back. But that does not work. Looks like there is only one option to destroy either the bullet or the trail object totally (using Destroy()) and instantiate a new bullet or attach a new trail object every time we're about to use a bullet. But that's not pooling technique used frequently in Unity, whereas we avoid using Destroy() as much as possible.

    I would like to hear some suggestion to solve this problem from you. It's not very serious because I always have another choice not using TrailRenderer at all. Thank you all.
     
  2. nikokoneko

    nikokoneko

    Joined:
    Feb 20, 2015
    Posts:
    35
    I've been using the reusable object pool of bullets with TrailRenderer in one of my games but I don't have the source code with me right now to check. I know what you mean, I had the same problem in the beginning, the mess happens because the trail is rendered from the position when your bullet "died" last time to the position where it appears on scene again. Maybe you should try Autodestruct feature, look it up in the manual. If the problem persists I'll respond again when I get to my studio tomorrow to check the code.
     
  3. VipHaLongPro

    VipHaLongPro

    Joined:
    Mar 8, 2015
    Posts:
    49
    Thank you for your quick reply. In fact I had tried checking the AutoDestruct option before but it seemed to have no effect. It's very kind of you to let me know how you did it. I'm sure this will also be very helpful to others.
     
  4. nikokoneko

    nikokoneko

    Joined:
    Feb 20, 2015
    Posts:
    35
    hm... I've just checked my project, unfortunately I can't run it because some plugins break in Unity 5.
    but I looked through the code and it appears i don't do anything with TrailRenderer. I _do_ have a slow TrailRenderer duration, though only 0.25s so it could be that I never pool the same object so fast (so the trail wears off in the meantime). FYI for object pooling I used this script: http://unitypatterns.com/resource/objectpool/
     
    VipHaLongPro likes this.
  5. VipHaLongPro

    VipHaLongPro

    Joined:
    Mar 8, 2015
    Posts:
    49
    Well, thanks to you I've realized that actually my bullets were reused too fast. The reloading rate is 0.1s, while the life time of the trail is 1s. I hoped disabling the TrailRenderer should disable it but looks like the only way is waiting for its life time going over. So delaying the recycling works.
     
  6. modori3232

    modori3232

    Joined:
    Jul 22, 2019
    Posts:
    1
    you can use "clear" method in TrailRenderer

    //example
    trailRenderer.Clear();