Search Unity

Bullet tracer annoyance

Discussion in 'Scripting' started by Lethn, Dec 22, 2017.

  1. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I made a thread about the topic of bullet tracers awhile back, I've tried tackling it again and it's this typical thing of programming where the theory behind it all makes perfect sense, instantiate the tracer particle, translate it to the hit.point of the raycast.

    I'm messing around and experimenting but it's pretty frustrating dealing with this when I know I'm so close, I followed a tutorial awhile ago to get bulletholes working. I like this bulletspread code a lot because of how flexible it is, lots of ways to make a variety of automatic weapons, I just need to get the damn tracers working the same way.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GunHit
    6.  
    7. {
    8.     public RaycastHit raycastHit;
    9. }
    10.  
    11. public class RaycastShoot : MonoBehaviour {
    12.  
    13.  
    14.     public float fireDelay = 0.6f;
    15.     public float maxBulletSpreadAngle = 15.0f;
    16.     public float damage = 1.0f;
    17.     public float timeUntilMaxSpreadAngle = 1.0f;
    18.     public float ExplosionForce = 1.0f;
    19.     public float ExplosionRadius = 3.0f;
    20.  
    21.     public GameObject bulletHolePrefab;
    22.     public float bulletHoleSizeWidthMin = 0.001f;
    23.     public float bulletHoleSizeWidthMax = 0.003f;
    24.  
    25.     public GameObject tracerParticle;
    26.  
    27.     private bool readyToFire = true;
    28.     private float fireTime;
    29.  
    30.     // Use this for initialization
    31.     void Start () {
    32.    
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update () {
    37.  
    38.  
    39.  
    40.         if (Input.GetMouseButton (0))
    41.    
    42.         {
    43.  
    44.             fireTime += Time.deltaTime;
    45.  
    46.  
    47.  
    48.             if (readyToFire)
    49.        
    50.             {
    51.  
    52.                 Vector3 fireDirection = transform.forward;
    53.                 Quaternion fireRotation = Quaternion.LookRotation (fireDirection);
    54.                 Quaternion randomRotation = Random.rotation;
    55.  
    56.                 float currentSpread = Mathf.Lerp (0.0f, maxBulletSpreadAngle, fireTime / timeUntilMaxSpreadAngle);
    57.  
    58.                 fireRotation = Quaternion.RotateTowards (fireRotation, randomRotation, Random.Range (0.0f, currentSpread));
    59.        
    60.                 RaycastHit hit;
    61.  
    62.  
    63.                 if (Physics.Raycast (transform.position, fireRotation * Vector3.forward, out hit, Mathf.Infinity))
    64.            
    65.                 {
    66.                     GunHit gunHit = new GunHit ();
    67.                     gunHit.damage = damage;
    68.                     gunHit.raycastHit = hit;
    69.  
    70.                     readyToFire = false;
    71.                     Invoke ("SetReadyToFire", fireDelay);
    72.                     Instantiate (Resources.Load ("BulletImpactParticle"), hit.point, Quaternion.LookRotation (hit.normal));
    73.  
    74.                     Instantiate (bulletHolePrefab, hit.point, Quaternion.FromToRotation ( Vector3.up, hit.normal ) );
    75.                     bulletHolePrefab.transform.localScale = new Vector3 ( 0.3381936f, 0.8108215f, 0.03285804f ) * Random.Range ( 0.5f, 2f );
    76.  
    77.                     Instantiate ( tracerParticle.transform, transform.position, transform.rotation );
    78.                     tracerParticle.transform.Translate ( hit.point );
    79.  
    80.  
    81.  
    82.                 }
    83.  
    84.  
    85.             }
    86.  
    87.         }
    88.  
    89.         else
    90.    
    91.         {
    92.             fireTime = 0.0f;
    93.         }
    94.  
    95.     }
    96.  
    97.  
    98.  
    99.     void SetReadyToFire ()
    100.  
    101.     {
    102.         readyToFire = true;
    103.     }
    104.  
    105.  
    106.  
    107.  
    108.  
    109. }
    110.  
    For the record I'm not using rigidbodies or anything, I just want to make the simplest tracers possible, very much like what you see in CS:GO, the video below is exactly what I'm thinking of, got the first part of the bulletholes right as I said but no luck yet with tracers.



    I did find an interesting post by BMRX on the topic but I don't really have any clue how to implement that properly either.

    https://forum.unity.com/threads/bullet-tracer-line-renderer-effect.323256/

    Should I perhaps be doing what he was doing and use a texture rather than a particle effect to make my tracers happen?
     
    Last edited: Dec 22, 2017
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    I see two problems right away, in two pairs of code above.

    Line 74 and line 75: you are instantiating a prefab, not keeping a reference to it, then changing a property of the prefab, NOT the instantiated thing.

    Line 77 and Line 78 - ditto.

    so the pattern you want is

    Code (csharp):
    1. GameObject ThisThingy = Instantiate( etc, etc, etc ) as GameObject;
    2. ThisThingy.transform.Translate( hitpoint);
    Does that make sense? You want to get a reference to the thing you instantiate and change THAT thing's transform.

    And that applies to both the bullethole and the tracer particle.
     
    Last edited: Dec 22, 2017
  3. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I get what you're saying, this error popped up however.

     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Weird: stealth edit above... the words .transform.Translate() disappeared on one of my edits. It looks like I fixed it now... sorry!

    (this referring to my line 2 above)
     
  5. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Your code makes perfect sense generally, but what is the hitpoint? Is this some sort of new raycast you've made elsewhere? Or is it something else?
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just a guess, but I assume he meant "hit.point" as he was just copying your code with a correction for the instantiated object (vs the prefab). :)
     
  7. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    hmm, well I got the code semi-working now, the bullet tracers are clearly going towards the hit.point, I enabled collision so that they wouldn't go through objects and hit the surface so that problem is fixed that I ran into. However, I think the movement is happening instantly as I can't even see the tracers moving during runtime.
     
  8. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Oh wait! I think I know what could possibly going on, for some reason despite me having the particle on loops there is absolutely nothing happening,

    YES! Worked it out! Just needed to enable play on awake!
     
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Though I had nothing to do with it working for you... I'm glad you got it working! lol :)
     
  10. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Well there is something you could help with if you want, I've run into an issue with getting the damn particles actually moving, depending on the code I use they instantiate right at the hit.point or when I try to use speed * Time.DeltaTime they instantiate just a metre in front of me.

    It's pretty obvious I'm using that code wrong, I don't mess with Translate very often, I should probably experiment with it after I'm done with this task. I am by the way of course trying to make these tracers match up with the bulletholes like the video I've shown, I get the feeling I need to make the rotation and so on work the same way for my particles too because the tracers are instantiating themselves in a bit of a weird way and sometimes get through the walls despite the collision.
     
  11. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Didn't quite follow that. Where do you want the particles to be?
    I don't even know what a "tracer" is lol Sorry! I mean I watched a min of the video and I hear shooting and see holes, but that's it.
    Maybe, maybe I might be able to help in some way, if I understood your goal/question :)
     
  12. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Oh sorry, they're those yellow/orange streaks from the bullets that you often see in games and tracer rounds exist in real life, one second, I'll get a more blatant example.



    What I'm doing is getting this effect for Unity, I was told awhile back the best way to do it is to instantiate the particle and then translate it towards the raycast, what's happening to me is that I get the particle just going straight to the hit.point and I want it to take time to travel there.

    Maybe I should upload a video of it happening on my project so people know what's going on.
     
  13. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I see. Well, turns out my guess was right as to what they were.. but now I'm not guessing, which is good :)
    For sure you could have them move towards. Like give them a destination on a script, and then get them to move towards that destination.
    I suppose, though, could you also make them a child object with a small offset from the bullet? That sounds workable (particle/trail renderer).
     
  14. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    That's almost working, I get your idea about a trail renderer and that would probably be the most effective thing but oddly the trailer renderer doesn't seem to be moving with the particle, both the particle and the trail just seem to be sort of frozen in place somehow.

    move towards is working very accurately though, am I doing this code wrong? It doesn't seem to be moving the particle so I probably am.

    Code (CSharp):
    1. tracerParticleObject.transform.position = Vector3.MoveTowards ( transform.position, hit.point, 2f );
    And yes, of course I am, the third variable is the distance.
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    My experience with trail renderers is limited, but I would assume that you can move them.
    As for your other code, it looks perfectly good, provided that it's running in update.
    If you have many tracers going though, I would imagine that it's easier to have a script on the tracer game object (with a destination variable) and then its own update can run it.
    As opposed to tracking them all from a shooting script or something..
     
  16. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I think this is the right idea but I'm still trying to get the actual particle to move, is there something extra I need? Is there some variable I need to add so it doesn't just move instantly towards the target? I think this will be the case as I've only got a distance variable, bah, I really should brush up on my knowledge of this.
     
  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I see no reason why you couldn't use the same idea for the particle as the tracer (unless that's what you meant - don't wanna get confused lol). If you want it to move like the tracer, the same code should do it?
     
  18. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Yes I'm really baffled by this, movetowards should be moving the particle, sorry for the confusion, the tracer is just a word for the effect I'm making, the particle is the object I'm moving, here's what I'm using now.

    Code (CSharp):
    1.                     GameObject tracerParticleObject = Instantiate ( tracerParticle, transform.position, transform.rotation ) as GameObject;
    2.  
    3.                     tracerParticleObject.transform.position = Vector3.MoveTowards ( transform.position, hit.point, speed * Time.deltaTime ) ;
    For whatever reason this code doesn't seem to be working right when it comes to the actual movement, instead of effecting the speed, it just changes the distance, this is meant to be correct so why isn't the particle moving towards hit.point?
     
  19. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    well, if your code looks like:
    Code (csharp):
    1.  
    2. void FireBullet() {
    3.    GameObject tracerParticleObject = Instantiate ( tracerParticle, transform.position, transform.rotation ) as GameObject;
    4.    tracerParticleObject.transform.position = Vector3.MoveTowards ( transform.position, hit.point, speed * Time.deltaTime ) ;
    5. }
    Then it only moves once.?
     
  20. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Nope, need the raycast remember because the particles have to match the bullet spread.
     
  21. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    hm.. Can you post the code that does the shot & the move towards? :)
     
  22. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    It's all in the OP but it has changed slightly, so I'll post it again.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GunHit
    6.  
    7. {
    8.     public float damage;
    9.  
    10.     public RaycastHit raycastHit;
    11. }
    12.  
    13. public class RaycastShoot : MonoBehaviour {
    14.  
    15.  
    16.     public float fireDelay = 0.6f;
    17.     public float maxBulletSpreadAngle = 15.0f;
    18.     public float damage = 1.0f;
    19.     public float timeUntilMaxSpreadAngle = 1.0f;
    20.     public float ExplosionForce = 1.0f;
    21.     public float ExplosionRadius = 3.0f;
    22.  
    23.     public GameObject bulletHolePrefab;
    24.     public float bulletHoleSizeWidthMin = 0.001f;
    25.     public float bulletHoleSizeWidthMax = 0.003f;
    26.  
    27.     public GameObject tracerParticle;
    28.  
    29.     private bool readyToFire = true;
    30.     private float fireTime;
    31.  
    32.     public float speed = 1.0f;
    33.  
    34.     // public float speed = 1.0f;
    35.  
    36.     // Use this for initialization
    37.     void Start () {
    38.      
    39.     }
    40.  
    41.     // Update is called once per frame
    42.     void Update () {
    43.  
    44.  
    45.  
    46.         if (Input.GetMouseButton (0))
    47.      
    48.         {
    49.  
    50.             fireTime += Time.deltaTime;
    51.  
    52.  
    53.  
    54.             if (readyToFire)
    55.          
    56.             {
    57.  
    58.                 Vector3 fireDirection = transform.forward;
    59.                 Quaternion fireRotation = Quaternion.LookRotation (fireDirection);
    60.                 Quaternion randomRotation = Random.rotation;
    61.  
    62.                 float currentSpread = Mathf.Lerp (0.0f, maxBulletSpreadAngle, fireTime / timeUntilMaxSpreadAngle);
    63.  
    64.                 fireRotation = Quaternion.RotateTowards (fireRotation, randomRotation, Random.Range (0.0f, currentSpread));
    65.          
    66.                 RaycastHit hit;
    67.  
    68.                 if (Physics.Raycast (transform.position, fireRotation * Vector3.forward, out hit, Mathf.Infinity))
    69.              
    70.                 {
    71.                     GunHit gunHit = new GunHit ();
    72.                     gunHit.raycastHit = hit;
    73.  
    74.                     readyToFire = false;
    75.                     Invoke ("SetReadyToFire", fireDelay);
    76.                     Instantiate (Resources.Load ("BulletImpactParticle"), hit.point, Quaternion.LookRotation (hit.normal));
    77.  
    78.                     Instantiate (bulletHolePrefab, hit.point, Quaternion.FromToRotation ( Vector3.up, hit.normal ) );
    79.                     bulletHolePrefab.transform.localScale = new Vector3 ( 0.3381936f, 0.8108215f, 0.03285804f ) * Random.Range ( 0.5f, 2f );
    80.  
    81.  
    82.  
    83.                     GameObject tracerParticleObject = Instantiate ( tracerParticle, transform.position, transform.rotation ) as GameObject;
    84.                     tracerParticleObject.transform.position = Vector3.MoveTowards ( transform.position, hit.point, speed * Time.deltaTime ) ;
    85.  
    86.                    
    87.  
    88.  
    89.  
    90.                 }
    91.  
    92.  
    93.             }
    94.  
    95.         }
    96.  
    97.         else
    98.      
    99.         {
    100.             fireTime = 0.0f;
    101.         }
    102.  
    103.     }
    104.  
    105.  
    106.  
    107.     void SetReadyToFire ()
    108.  
    109.     {
    110.         readyToFire = true;
    111.     }
    112.  
    113.  
    114.  
    115.  
    116.  
    117. }
    118.  
     
  23. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Right, sorry about the OP.. i sometimes forget the world while I'm responding here.
    However, your post shows what I've been trying to say to you for the last few posts and why there is a problem.
    It is only moving 1 "slice". This is why I suggested the script on the particle (with a destination), so that it can... continue .. to move towards the target, in its own Update method. :)
    Does that make sense?
     
  24. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    That does make sense, but how will I keep the accuracy and have it move towards the hit.point? Does this mean I have to make a whole new raycast for that particular particle?
     
  25. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    hm.. Okay, I was trying to say, like.
    Code (csharp):
    1.  
    2. public class TracerScript : MonoBehaviour {
    3.    public Vector3 destination;
    4.  
    5.  void Update() {
    6.     transform.position = Vector3.MoveTowards(transform.position, destination, 2f);
    7.   }
    8. }
    9. // in the code with the bullet creation
    10.   // not your real variable names, but you get the idea..
    11.   GameObject bullet = Instantiate(whatever);
    12.   bullet.GetComponent<TracerScript>().destination = hit.point;
    13.  
    14.  
     
    Lethn likes this.
  26. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I can see that working but can I ask why you've instantiated a gameobject there?
     
  27. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, I haven't.. sorry, guess you didn't read my comment ;)

    That is meant as an example for the code you already have that instantiates the prefab. It's there only to correlate/show the part about assigning the destination variable :)
     
  28. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Ugh, I now remember why I avoided trying to tackle this problem for so long.

    Just to clarify, I did read your comments, just going to have to work through it for the moment to apply it to my own project properly.
     
  29. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, well hope it works for ya. I mean, at least you can test the idea/theory and maybe it's good or not.. It seems sensible to me. So hopefully good things will happen. :)
     
  30. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Yeah, hang on, I just figured out why I was having trouble, for some reason even though I'm doing GetComponent, hit.point is not coming up for me, the compiler only seems to be detecting GunHit?
     
  31. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm not familiar with the Gunhit type, but you do assign 'hit' to it's field/property. So long as you're inside the raycast scope of the code, you should have access to "hit.point".
    I hope you figure it out. I won't be back for a number of hours, but next time I'm here I'll see what you got :)
     
  32. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    May have something to do with the way I'm using GetComponent, thanks for your help, the code does make sense, so I'll probably just need to work it all out for my own project.
     
  33. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  34. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I can at least explain my problem now, and it was thanks to you mentioning the raycast scope, because I'm on an entirely different script for whatever reason Unity isn't detecting the raycast that I made in RaycastShoot and that's why I can't seem to use hit.point the way you are.

    That's why I think only GunHit is showing up when I use GetComponent, I obviously need to use GunHit and then hit.point somehow otherwise the compiler just complains if I use only hit.point.

    If anyone can help me figure out this last bit that would be great, think I'm almost there now.
     
  35. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Not sure what you mean about Unity isn't detecting the raycast.
    As for GetComponent, just to clear up some fuzziness in your wording.. what exactly are you trying to type?
    You should be looking for a component type (same as a new script you made, like in my example?). Maybe what you said and how I read it are two totally different things, though.

    I'm truly confused how it can not 'detect' the raycast, yet you are assigning to GunHit in that same scope..

    I was leaving, and then came back.. now leaving , again. Need some sleep lol.

    Truly hope you get that sorted out. Sorry for the confusion on trying to understand what you were saying, as I couldn't really offer any decent feedback..
     
  36. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    If you need sleep go, I should post my code up so it makes sense for you if you're sticking around though.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class moveTowardsBulletTracer : MonoBehaviour {
    6.  
    7.     public Vector3 destination;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.      
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.  
    17.         GetComponent<RaycastShoot>().destination = GunHit  // hit.point does not show up when I type, only GunHit
    18.  
    19.         transform.position = Vector3.MoveTowards ( transform.position, destination, 1000 );
    20.  
    21.     }
    22. }
    23.  
    Does that help at all?
     
  37. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That's a little backwards from what I had been talking about. I was suggesting that after you instantiate the tracer in your RaycastShoot script, that you get the component on the new game object and set the destination variable (from there). :)
     
  38. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Hang on, I don't think that would work, my RaycastShoot script is attached to an empty that's on the muzzle of my gun, if I attached the script to the bullet, then it would simply duplicate all of this code twice. That's why I made a separate script just for the bullet, because then it would separate everything am I misunderstanding something here again?
     
  39. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I am only talking about the one line of code that gets the component and sets the destination (or 2 lines if you break it up). Not the whole thing .. that script is good on the bullet.
     
  40. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Any luck for you with this? :)
     
  41. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Oh! Hang on, my brain I think just clicked on it, now I get where you're going with this, I hate it when it's right in front of your face, let me just try writing it up.
     
  42. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    :) Sure.. no problem.
     
  43. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Hmmm, I totally get where you were going with this now but unfortunately MoveTowards is still pretty much instantiating on the place I click rather than moving towards it which is the main problem, which is odd, because by all accounts this should be working.
     
  44. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Be sure you adjust the value from your example (1000) as that is ... super fast. :)
     
  45. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    OH! That's all working now! Oh for crying out loud, so all along it was just the values needing adjustment for the scene I'm in depending on what I want it to look like now that I understand the code. Thanks, that seems to have genuinely done it, by the way, I had no idea you could assign values the way you did so I'm going to have to look that one up so I understand things better.

    Nice, I'll be able to make all kinds of effects with this, I had no idea you could use GetComponent in reverse, that's what threw me off I think.
     
  46. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Glad you got it working :) Enjoy your game & effects. =)


    Well, GetComponent works for the game object. When you use it without any value in front, eg: GetComponent<Something>(); the 'gameObject' (current one) is implied. If you have a different game object accessing get component, then it looks on that object.
     
  47. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I decided to try and tweak my bullet tracer code a bit to give it a bit more flexibility because I want to be able to easily switch in and out the particle effects for the tracers and I swear all I've done is changed the particle effect to a public gameobject so that I can swap particles in and out easily for each gun I have.

    However, for whatever reason unity has decided to instantiate the particles at 0, 0, 0, now obviously from the code I have this shouldn't happen as the Raycast shoot code should be grabbing the destination just fine.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GunHit
    6.  
    7. {
    8.     public float damage;
    9.  
    10.     public RaycastHit raycastHit;
    11. }
    12.  
    13. public class RaycastShoot : MonoBehaviour {
    14.  
    15.  
    16.     public float fireDelay = 0.6f;
    17.     public float maxBulletSpreadAngle = 15.0f;
    18.     public float timeUntilMaxSpreadAngle = 1.0f;
    19.  
    20.     public GameObject bulletHolePrefab;
    21.     public float bulletHoleSizeWidthMin = 0.001f;
    22.     public float bulletHoleSizeWidthMax = 0.003f;
    23.  
    24.     public GameObject tracerParticle;
    25.  
    26.     private bool readyToFire = true;
    27.     private float fireTime;
    28.  
    29.     public float speed = 1.0f;
    30.  
    31.     public Vector3 destination;
    32.  
    33.     // public float speed = 1.0f;
    34.  
    35.     // Use this for initialization
    36.     void Start () {
    37.    
    38.     }
    39.  
    40.     // Update is called once per frame
    41.     void Update () {
    42.  
    43.         if (Input.GetMouseButton (0))
    44.    
    45.         {
    46.  
    47.             fireTime += Time.deltaTime;
    48.  
    49.  
    50.  
    51.             if (readyToFire)
    52.        
    53.             {
    54.  
    55.                 Vector3 fireDirection = transform.forward;
    56.                 Quaternion fireRotation = Quaternion.LookRotation (fireDirection);
    57.                 Quaternion randomRotation = Random.rotation;
    58.  
    59.                 float currentSpread = Mathf.Lerp (0.0f, maxBulletSpreadAngle, fireTime / timeUntilMaxSpreadAngle);
    60.  
    61.                 fireRotation = Quaternion.RotateTowards (fireRotation, randomRotation, Random.Range (0.0f, currentSpread));
    62.        
    63.                 RaycastHit hit;
    64.  
    65.                 if (Physics.Raycast (transform.position, fireRotation * Vector3.forward, out hit, Mathf.Infinity))
    66.            
    67.                 {
    68.                     GunHit gunHit = new GunHit ();
    69.                     gunHit.raycastHit = hit;
    70.  
    71.                     readyToFire = false;
    72.                     Invoke ("SetReadyToFire", fireDelay);
    73.                     Instantiate (tracerParticle, hit.point, Quaternion.LookRotation (hit.normal));
    74.  
    75.                     Instantiate (bulletHolePrefab, hit.point, Quaternion.FromToRotation ( Vector3.up, hit.normal ) );
    76.                     bulletHolePrefab.transform.localScale = new Vector3 ( 0.3381936f, 0.8108215f, 0.03285804f ) * Random.Range ( 0.5f, 2f );
    77.  
    78.  
    79.  
    80.                     GameObject tracerParticleObject = Instantiate ( tracerParticle, transform.position, transform.rotation ) as GameObject;
    81.                     tracerParticleObject.GetComponent<moveTowardsBulletTracer>().destination = hit.point;
    82.  
    83.                     Debug.DrawRay ( transform.position, hit.point, Color.red, 1000 );
    84.  
    85.                     if ( hit.rigidbody )
    86.  
    87.                     {
    88.                         hit.rigidbody.AddForceAtPosition ( transform.forward * 50, hit.point );
    89.                     }
    90.  
    91.  
    92.                 }
    93.  
    94.  
    95.             }
    96.  
    97.         }
    98.  
    99.         else
    100.    
    101.         {
    102.             fireTime = 0.0f;
    103.         }
    104.  
    105.     }
    106.  
    107.  
    108.  
    109.     void SetReadyToFire ()
    110.  
    111.     {
    112.         readyToFire = true;
    113.     }
    114.  
    115.  
    116.  
    117.  
    118.  
    119. }
    120.  
    Tried a few tweaks but it's really misbehaving these days for some reason, for the record, the particles are firing directly upwards and they aren't shooting from the empty that I have attached to my gun's barrel.
     
  48. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Not sure what might be going wrong. Try and stick a Debug.Break(); statement somewhere inside of the shoot routine and when you shoot, it will pause the editor. That will let you go study the freshly-created GameObject and find out what is going on. If it's not where you want it to be, move it to some ridiculously wrong directly and see if it moves. If it doesn't move, use GameObject.CreatePrimitive() to spawn a sphere at that location, see where it goes in the scene when you pause at fire.
     
  49. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Been a bit busy with my other project but I'm going to focus a bit on this one now, I have actually already checked out what's going on with the particle effect and what's happening is for whatever reason the particle system is ignoring the direction code that grabs the hit.point and it instantiates exactly at 0, 0, 0.

    This of course shouldn't happen, didn't need the debug.log in the end to work out where it was going.
     
  50. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    If you Debug.Log() the value of hit.point, what is it when the particle is created?