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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

I am using raycast for my gun system. How would I apply a delay for raycast bullet hit?

Discussion in 'Scripting' started by the_Bad_Brad, Jul 17, 2018.

  1. the_Bad_Brad

    the_Bad_Brad

    Joined:
    Nov 2, 2014
    Posts:
    278
    For example, the player is shooting a subsonic weapon such as a sub-machinegun. The bullet impact should be slightly delayed when it hits a distant target.

    I tried using co-routines but it does not always work.

    Here is a code for one of the subsonic weapons:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Shockhammer : MonoBehaviour {
    6. public bool isFiring = false;
    7. public bool isIdle = false;
    8. public GameObject rifletip1;
    9. public GameObject tracertip1;
    10. public GameObject rifletip2;
    11. public GameObject tracertip2;
    12. public GameObject rifletip3;
    13. public GameObject tracertip3;
    14. public GameObject rifletip4;
    15. public GameObject tracertip4;
    16. public GameObject rifletip5;
    17. public GameObject tracertip5;
    18. public ParticleSystem muzzle;
    19.  
    20. private float timestamp;
    21. public float timeBetweenShots = 0.05f;
    22. public GameObject bullet_stone;
    23. public GameObject bullet_metal;
    24. public GameObject bullet_water;
    25. public GameObject bullet_flesh;
    26. public GameObject bullet_wood;
    27. public GameObject fire_sound;
    28. public GameObject tracer;
    29.  
    30. public GameObject suit;
    31.   public GameObject bhole_concrete;
    32.  
    33.   public AudioClip fire;
    34.  
    35. public GameObject shotgun_fire;
    36.  
    37.     AudioSource audioSource;
    38.     // Use this for initialization
    39.    
    40.     void Start () {
    41.         // AnimRefObj =suit.GetComponent<Animator>();
    42.         audioSource = fire_sound.GetComponent<AudioSource>();
    43.        
    44.     }
    45.    
    46.     // Update is called once per frame
    47.     void Update () {
    48.         if (isFiring)
    49.            
    50.             {
    51.                
    52.                 if(Time.time >= timestamp)
    53.                 {
    54.                    
    55.                         Instantiate(tracer, tracertip1.transform.position, tracertip1.transform.rotation);
    56.                         Instantiate(tracer, tracertip2.transform.position, tracertip2.transform.rotation);
    57.                         Instantiate(tracer, tracertip3.transform.position, tracertip3.transform.rotation);
    58.                         Instantiate(tracer, tracertip4.transform.position, tracertip4.transform.rotation);
    59.                         Instantiate(tracer, tracertip5.transform.position, tracertip5.transform.rotation);
    60.                         shotgun_fire.transform.Rotate(Vector3.forward * 300 * Time.deltaTime);
    61.                        
    62.                        
    63.                              audioSource.PlayOneShot(fire);
    64.         muzzle.Play();
    65.                    
    66.                    
    67.         RaycastHit hit;
    68.        
    69.        
    70.         timestamp = Time.time + timeBetweenShots;
    71.       if (Physics.Raycast(rifletip1.transform.position, rifletip1.transform.forward, out hit))
    72.         {
    73.            
    74.         if (hit.collider.gameObject.tag == "Stone")
    75.          {
    76.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    77.         Instantiate(bullet_stone, hit.point, hitRotation);
    78.         Instantiate(bhole_concrete, hit.point, hitRotation);
    79.        
    80.        
    81.  
    82.      }
    83.      if (hit.collider.gameObject.tag == "Armor")
    84.          {
    85.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    86.         Instantiate(bullet_metal, hit.point, hitRotation);
    87.         Instantiate(bhole_concrete, hit.point, hitRotation);
    88.        
    89.  
    90.      }
    91.      if (hit.collider.gameObject.tag == "Water")
    92.          {
    93.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    94.         Instantiate(bullet_water, hit.point, hitRotation);
    95.        
    96.        
    97.        
    98.  
    99.      }
    100.      if (hit.collider.gameObject.tag == "Flesh")
    101.          {
    102.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    103.         Instantiate(bullet_flesh, hit.point, hitRotation);
    104.        
    105.        
    106.        
    107.  
    108.      }
    109.      if (hit.collider.gameObject.tag == "Wood")
    110.          {
    111.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    112.         Instantiate(bullet_wood, hit.point, hitRotation);
    113.         Instantiate(bhole_concrete, hit.point, hitRotation);
    114.        
    115.        
    116.  
    117.      }
    118.      if (hit.collider.gameObject.name == "bandit") {
    119.             hit.collider.gameObject.SendMessage("hittorso");
    120.      }
    121.       if (hit.collider.gameObject.name == "Extinguisher") {
    122.             hit.collider.gameObject.GetComponent<exp_fire_ext>().enabled = true;
    123.      }
    124.      if (hit.collider.gameObject.name == "oildrum") {
    125.             hit.collider.gameObject.GetComponent<exp_oildrum>().enabled = true;
    126.      }
    127.         }
    128.    
    129.      //tip2
    130.    
    131.    
    132.      if (Physics.Raycast(rifletip2.transform.position, rifletip2.transform.forward, out hit))
    133.         {
    134.            
    135.         if (hit.collider.gameObject.tag == "Stone")
    136.          {
    137.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    138.         Instantiate(bullet_stone, hit.point, hitRotation);
    139.         Instantiate(bhole_concrete, hit.point, hitRotation);
    140.        
    141.        
    142.  
    143.      }
    144.      if (hit.collider.gameObject.tag == "Armor")
    145.          {
    146.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    147.         Instantiate(bullet_metal, hit.point, hitRotation);
    148.         Instantiate(bhole_concrete, hit.point, hitRotation);
    149.        
    150.  
    151.      }
    152.      if (hit.collider.gameObject.tag == "Water")
    153.          {
    154.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    155.         Instantiate(bullet_water, hit.point, hitRotation);
    156.        
    157.        
    158.        
    159.  
    160.      }
    161.      if (hit.collider.gameObject.tag == "Flesh")
    162.          {
    163.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    164.         Instantiate(bullet_flesh, hit.point, hitRotation);
    165.        
    166.        
    167.        
    168.  
    169.      }
    170.      if (hit.collider.gameObject.tag == "Wood")
    171.          {
    172.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    173.         Instantiate(bullet_wood, hit.point, hitRotation);
    174.         Instantiate(bhole_concrete, hit.point, hitRotation);
    175.        
    176.        
    177.  
    178.      }
    179.      if (hit.collider.gameObject.name == "bandit") {
    180.             hit.collider.gameObject.SendMessage("hittorso");
    181.      }
    182.       if (hit.collider.gameObject.name == "Extinguisher") {
    183.             hit.collider.gameObject.GetComponent<exp_fire_ext>().enabled = true;
    184.      }
    185.      if (hit.collider.gameObject.name == "oildrum") {
    186.             hit.collider.gameObject.GetComponent<exp_oildrum>().enabled = true;
    187.      }
    188.         }
    189.    
    190.    
    191.      //tip3
    192.    
    193.    
    194.      if (Physics.Raycast(rifletip3.transform.position, rifletip3.transform.forward, out hit))
    195.         {
    196.            
    197.         if (hit.collider.gameObject.tag == "Stone")
    198.          {
    199.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    200.         Instantiate(bullet_stone, hit.point, hitRotation);
    201.         Instantiate(bhole_concrete, hit.point, hitRotation);
    202.        
    203.        
    204.  
    205.      }
    206.      if (hit.collider.gameObject.tag == "Armor")
    207.          {
    208.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    209.         Instantiate(bullet_metal, hit.point, hitRotation);
    210.         Instantiate(bhole_concrete, hit.point, hitRotation);
    211.        
    212.  
    213.      }
    214.      if (hit.collider.gameObject.tag == "Water")
    215.          {
    216.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    217.         Instantiate(bullet_water, hit.point, hitRotation);
    218.        
    219.        
    220.        
    221.  
    222.      }
    223.      if (hit.collider.gameObject.tag == "Flesh")
    224.          {
    225.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    226.         Instantiate(bullet_flesh, hit.point, hitRotation);
    227.        
    228.        
    229.        
    230.  
    231.      }
    232.      if (hit.collider.gameObject.tag == "Wood")
    233.          {
    234.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    235.         Instantiate(bullet_wood, hit.point, hitRotation);
    236.         Instantiate(bhole_concrete, hit.point, hitRotation);
    237.        
    238.        
    239.  
    240.      }
    241.      if (hit.collider.gameObject.name == "bandit") {
    242.             hit.collider.gameObject.SendMessage("hittorso");
    243.      }
    244.       if (hit.collider.gameObject.name == "Extinguisher") {
    245.             hit.collider.gameObject.GetComponent<exp_fire_ext>().enabled = true;
    246.      }
    247.      if (hit.collider.gameObject.name == "oildrum") {
    248.             hit.collider.gameObject.GetComponent<exp_oildrum>().enabled = true;
    249.      }
    250.         }
    251.    
    252.      //tip4
    253.    
    254.    
    255.    
    256.    
    257.     if (Physics.Raycast(rifletip4.transform.position, rifletip4.transform.forward, out hit))
    258.         {
    259.            
    260.         if (hit.collider.gameObject.tag == "Stone")
    261.          {
    262.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    263.         Instantiate(bullet_stone, hit.point, hitRotation);
    264.         Instantiate(bhole_concrete, hit.point, hitRotation);
    265.        
    266.        
    267.  
    268.      }
    269.      if (hit.collider.gameObject.tag == "Armor")
    270.          {
    271.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    272.         Instantiate(bullet_metal, hit.point, hitRotation);
    273.         Instantiate(bhole_concrete, hit.point, hitRotation);
    274.        
    275.  
    276.      }
    277.      if (hit.collider.gameObject.tag == "Water")
    278.          {
    279.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    280.         Instantiate(bullet_water, hit.point, hitRotation);
    281.        
    282.        
    283.        
    284.  
    285.      }
    286.      if (hit.collider.gameObject.tag == "Flesh")
    287.          {
    288.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    289.         Instantiate(bullet_flesh, hit.point, hitRotation);
    290.        
    291.        
    292.        
    293.  
    294.      }
    295.      if (hit.collider.gameObject.tag == "Wood")
    296.          {
    297.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    298.         Instantiate(bullet_wood, hit.point, hitRotation);
    299.         Instantiate(bhole_concrete, hit.point, hitRotation);
    300.        
    301.        
    302.  
    303.      }
    304.      if (hit.collider.gameObject.name == "bandit") {
    305.             hit.collider.gameObject.SendMessage("hittorso");
    306.      }
    307.       if (hit.collider.gameObject.name == "Extinguisher") {
    308.             hit.collider.gameObject.GetComponent<exp_fire_ext>().enabled = true;
    309.      }
    310.      if (hit.collider.gameObject.name == "oildrum") {
    311.             hit.collider.gameObject.GetComponent<exp_oildrum>().enabled = true;
    312.      }
    313.    
    314.         }
    315.    
    316.      //tip5
    317.    
    318.      if (Physics.Raycast(rifletip5.transform.position, rifletip5.transform.forward, out hit))
    319.         {
    320.            
    321.         if (hit.collider.gameObject.tag == "Stone")
    322.          {
    323.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    324.         Instantiate(bullet_stone, hit.point, hitRotation);
    325.         Instantiate(bhole_concrete, hit.point, hitRotation);
    326.        
    327.        
    328.  
    329.      }
    330.      if (hit.collider.gameObject.tag == "Armor")
    331.          {
    332.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    333.         Instantiate(bullet_metal, hit.point, hitRotation);
    334.         Instantiate(bhole_concrete, hit.point, hitRotation);
    335.        
    336.  
    337.      }
    338.      if (hit.collider.gameObject.tag == "Water")
    339.          {
    340.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    341.         Instantiate(bullet_water, hit.point, hitRotation);
    342.        
    343.        
    344.        
    345.  
    346.      }
    347.      if (hit.collider.gameObject.tag == "Flesh")
    348.          {
    349.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    350.         Instantiate(bullet_flesh, hit.point, hitRotation);
    351.        
    352.        
    353.        
    354.  
    355.      }
    356.      if (hit.collider.gameObject.tag == "Wood")
    357.          {
    358.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    359.         Instantiate(bullet_wood, hit.point, hitRotation);
    360.         Instantiate(bhole_concrete, hit.point, hitRotation);
    361.        
    362.        
    363.  
    364.      }
    365.      if (hit.collider.gameObject.name == "bandit") {
    366.             hit.collider.gameObject.SendMessage("hittorso");
    367.      }
    368.       if (hit.collider.gameObject.name == "Extinguisher") {
    369.             hit.collider.gameObject.GetComponent<exp_fire_ext>().enabled = true;
    370.      }
    371.      if (hit.collider.gameObject.name == "oildrum") {
    372.             hit.collider.gameObject.GetComponent<exp_oildrum>().enabled = true;
    373.      }
    374.    
    375.    
    376.    
    377.    
    378.      }
    379.    
    380.    
    381.    
    382.    
    383.    
    384.  
    385.    
    386.    
    387.    
    388.    
    389.    
    390.    
    391.    
    392.                
    393.    
    394.    
    395.    
    396.      if (isIdle)
    397.            
    398.             {
    399.                
    400.             }
    401.    
    402.         }
    403.             }
    404.     }
    405.            
    406.         public void onIdle()
    407.      {
    408.        isFiring=false;
    409.        isIdle=true;
    410.      
    411.      
    412.      }  
    413.            
    414.             public void onFiring()
    415.      {
    416.        isFiring=true;
    417.        isIdle=false;
    418.      
    419.      
    420.      }
    421. }
    422.    
    423.  
    424.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    A common subsonic 9mm round goes at 300 m/s, which is 5 meters per frame at 60fps, or 10 meters per frame at 30fps.

    Why don't you just create a moving round and make a script that moves the bullet from the tip of the gun and raycasts ahead of it the required distance each frame, and when it finally hits something you can make a little puff of smoke. That way if you fire it and the target quickly leaps aside, you will truly miss it. Slower rounds will have to be aimed ahead.

    And don't forget gravity accelerating the bullet downward over time, because subsonic rounds have more-noticeable drop due to their lower speed. Further targets might require more elevation to hit them.

    And then while you're at it, might as well slow the round down every frame by whatever amount you want to consider, as the moment the bullet leaves the gun it begins decelerating rapidly due to air resistance.
     
  3. the_Bad_Brad

    the_Bad_Brad

    Joined:
    Nov 2, 2014
    Posts:
    278
    Alright it worked. I simply modified my bullet loco script. Now the raycast begins from the tracer tip rather than the gun. To prevent the raycast from repeating, I simply disable the script as soon as the ratcast hits an object within 2 inches in front of it. Now it behaves more like a real bullet. I just have to make the projectile react with gravity now to simulate bullet drop on longer distances.




    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class plas_loco : MonoBehaviour {
    6. public GameObject bullet_stone;
    7. public GameObject bullet_metal;
    8. public GameObject bullet_water;
    9. public GameObject bullet_flesh;
    10. public GameObject bullet_wood;
    11. public GameObject btip;
    12. public GameObject bhole_concrete;
    13.     // Use this for initialization
    14.     void Start () {
    15.        
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update () {
    20.          transform.Translate(10.0f * Time.deltaTime, 0,0 );
    21.        
    22.        
    23.          RaycastHit hit;
    24.          if (Physics.Raycast(btip.transform.position, btip.transform.forward, out hit, 2))
    25.         {
    26.              //print("Found an object - distance: " + hit.distance);
    27.         if (hit.collider.gameObject.tag == "Stone")
    28.          {
    29.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    30.         Instantiate(bullet_stone, hit.point, hitRotation);
    31.         Instantiate(bhole_concrete, hit.point, hitRotation);
    32.            Destroy(this);
    33.        
    34.  
    35.      }
    36.      if (hit.collider.gameObject.tag == "Armor")
    37.          {
    38.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    39.         Instantiate(bullet_metal, hit.point, hitRotation);
    40.         Instantiate(bhole_concrete, hit.point, hitRotation);
    41.            Destroy(this);
    42.  
    43.      }
    44.      if (hit.collider.gameObject.tag == "Water")
    45.          {
    46.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    47.         Instantiate(bullet_water, hit.point, hitRotation);
    48.            Destroy(this);
    49.        
    50.        
    51.  
    52.      }
    53.      if (hit.collider.gameObject.tag == "Flesh")
    54.          {
    55.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    56.         Instantiate(bullet_flesh, hit.point, hitRotation);
    57.        
    58.            Destroy(this);
    59.        
    60.  
    61.      }
    62.      if (hit.collider.gameObject.tag == "Wood")
    63.          {
    64.         var hitRotation=Quaternion.FromToRotation(Vector3.forward,hit.normal);
    65.         Instantiate(bullet_wood, hit.point, hitRotation);
    66.         Instantiate(bhole_concrete, hit.point, hitRotation);
    67.            Destroy(this);
    68.        
    69.  
    70.      }
    71.      if (hit.collider.gameObject.name == "bandit") {
    72.             hit.collider.gameObject.SendMessage("hittorso");
    73.      }
    74.       if (hit.collider.gameObject.name == "Extinguisher") {
    75.             hit.collider.gameObject.GetComponent<exp_fire_ext>().enabled = true;
    76.                Destroy(this);
    77.      }
    78.      if (hit.collider.gameObject.name == "oildrum") {
    79.             hit.collider.gameObject.GetComponent<exp_oildrum>().enabled = true;
    80.                Destroy(this);
    81.      }
    82.    
    83.      }
    84.        
    85.        
    86.     }
    87. }
    88.  
     
    Kurt-Dekker likes this.
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Hey that looks great. Nice work!

    To do gravity you basically need the notion of a Vector3 variable that accumulates from its original value of Vector3.zero each frame, and then is added to the transform.Translate() argument.

    If you make a class member variable and gravity constant like so (outside of any function):

    Code (csharp):
    1. Vector3 drop;
    2. float GravityAmount = 9.81f;
    and then replace line 20 above with something like:

    Code (csharp):
    1.  drop += Vector3.down * GravityAmount * Time.deltaTime;
    2.  Vector3 movement = new Vector3( 10.0f * Time.deltaTime, 0,0);
    3.  movement += drop;
    4.  transform.Translate(movement);
    That should give you some interesting bullet drop. As a member variable, drop will already be set to Vector3.zero on class creation.