Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

C# Help raycasting with a "Fire rate"

Discussion in 'Scripting' started by Kazzack, Feb 6, 2014.

  1. Kazzack

    Kazzack

    Joined:
    Mar 8, 2013
    Posts:
    27
    Hey guys. Got myself a nice working raycasting "shooting" script.

    Code (csharp):
    1.  
    2. public bool firing = false;
    3. public float fireRate = 0.1f;
    4.  
    Code (csharp):
    1.  
    2. if(Input.GetButtonDown("Fire1"))
    3.         {          
    4.             if(canShoot == true)
    5.             {
    6.                 Screen.lockCursor = true;
    7.                 Ray mouseRay = Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
    8.                 RaycastHit hitInfo;
    9.            
    10.                 if(Physics.Raycast(mouseRay, out hitInfo))
    11.                 {
    12.                     Debug.Log(hitInfo.transform.name);
    13.                     curAmmoInClip -= 1;
    14.                     Health enemyHealth = hitInfo.transform.GetComponent<Health>();
    15.                     if(enemyHealth !=null)
    16.                     {
    17.                         enemyHealth.Damage(damage);
    18.                     }
    19.                 }
    20.             }
    21.         }
    22.  
    Basically I would like to know how I could set up a "fire rate", so that the raycasts only shoot out when it is allowed to due to the fire rate. I know it's something to do with time.deltatime, but i can't work out how quite to do it, or where i would even add it in my gun script.

    I guess another way to describe it would be a timed delay of when the rays can get sent out... I'm not too sure how to word it :p
    I hope that makes sense?

    thanks in advanced :)

    BONUS help ;)
    (Isn't as important as the above bit as i have not got round to researching this bit yet, but if your willing to help and point me in the right direction, i'm not going to complain xD)
    I'm not too sure how i would go about adding sound to each "ray" that is cast out or even to instantiate a prefab / texture where the ray lands, or instantiate a different one depending on the layer the raycast has hit... But i'll do some more research on this before i ask for help :)
     
    Last edited: Feb 6, 2014
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    where are you setting "canShoot"?

    if you add in a variable to store the last time the weapon fired you can include a

    "if weaponlastfired + delaytime > current time then canshoot = false"

    statement to prevent the weapon firing too soon.


    (example similar to this on the Time.time reference page: http://docs.unity3d.com/Documentation/ScriptReference/Time-time.html)

    bonus: googling "unity play audio" comes up with loads of answers, maybe have a look at the AudioSource component pages too.
     
  3. Khazzack

    Khazzack

    Joined:
    Sep 27, 2013
    Posts:
    29
    canShoot is being set up in Update function. I got it so that if i run out of ammo in a "clip" then i can't shoot, also got other stuff like if im reloading etc.... but that's not really an issue,

    hmmm... okay i'll see what i can do. thanks for the help.

    P.S. I am the same guy I must of logged in on a different accounts :p
     
    Last edited: Feb 6, 2014