Search Unity

Bullet tracer/line renderer effect

Discussion in 'Scripting' started by BMRX, May 3, 2015.

  1. BMRX

    BMRX

    Joined:
    Nov 23, 2014
    Posts:
    51
    So I've been struggling to create some sort of way to create a bullet tracer effect.

    I discovered the line renderer and it seems to be able to create the exact effect I want:

    linerenderer.png
    linerendererinspector.png

    My problem is that I cannot for the life of me figure out how to make it start from my gun and fly towards my the hit.point of my raycast(s).

    Code (csharp):
    1.  
    2. private GameObject bulletSprayPrefab; //Bullet Particle
    3.  
    4. void Start(){
    5. bulletSprayPrefab = GameObject.Find("bulletTracer");
    6. }
    7.  
    8. void ShootRay()
    9.   {
    10.   //  Generate a random XY point inside a circle:
    11.   Vector3 direction = Random.insideUnitCircle / scaleLimit;
    12.   direction.z = z; // circle is at Z units
    13.   direction = transform.TransformDirection(direction.normalized);
    14.   foundHit = Physics.Raycast(transform.position, direction, out hit, 100);  //Raycast
    15.   // GameVariables.clipSize--;
    16.   Instantiate(bulletSprayPrefab, spawnPoint.transform.position + direction, transform.rotation); //create particle
    17.   bulletSprayPrefab.GetComponent<Rigidbody>().AddForce(transform.forward * 100000);
    18.   }
    This makes the line render appear as I have it created in the scene view, but of course just falls to the ground. I assume that it is creating one for every raycast there is mostly because I have not devised a way to determine a rate of fire for the weapon yet so these things are created quite quickly. (facing the wrong way, but they pour out of the barrel non-the-less)

    ShootRay(); is called in Update with Input.GetMouseDown(0) if more of the class is needed just let me know.

    EDIT/
    (skip to 1:10 for the effect I'm trying to create.)
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    Have you tried the Trail Renderer?
     
    BMRX likes this.
  3. BMRX

    BMRX

    Joined:
    Nov 23, 2014
    Posts:
    51
    Not until you suggested, no luck. This is probably so simple it's laughable... Like most of the other issues I run into. Usually I answer my problem before someone has time to answer. Thanks Renman!
     
    renman3000 likes this.
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    It's a very simple component.
     
  5. BMRX

    BMRX

    Joined:
    Nov 23, 2014
    Posts:
    51
    Indeed, it seems to be. I tried instantiating it on hit.point of my raycasts yet... Nothing, no effect just a glowing effect where it should spawn at the barrel of my gun and I only noticed that effect because of how shiny one of my objects was. :p

    EDIT/ I'm putting it on hold for now, the frustration associated with this is too high. I'll be back bullet tracer, mark my words.
     
    Last edited: May 3, 2015
  6. BMRX

    BMRX

    Joined:
    Nov 23, 2014
    Posts:
    51
    Code (csharp):
    1.  
    2. private GameObject tracer;
    3. private float thrust;
    4. clone = (Transform)Instantiate(bulletTracer.transform, transform.position, transform.rotation);
    5. clone.GetComponent<Rigidbody>().AddForce(hit.point * thrust);
    6.  
    Didn't know that I had to put the .transform on the object I was instantiating.... There is no mention of that anywhere that I can find.

    If I assign clone in Start() I can only instantiate the object once,
    I see the effect and then that is it.

    If I assign it on Update() you can see the object being created in the hierarchy every shot but there is no effect at all.

    Still frustrating but at least it's getting figured.
     
    renman3000 likes this.