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

How to Make An Assault Rifle Fire automatic?

Discussion in 'Scripting' started by ColouredPixelsUnity, Sep 14, 2015.

  1. ColouredPixelsUnity

    ColouredPixelsUnity

    Joined:
    Aug 10, 2014
    Posts:
    78
    Hi! Me and a couple friends are planning to make a FPS game. We are somewhat new to Unity. I know some C#, but I was wondering how I can make he gun shoot at a certain pace (depending on the guns fire rate is how fast it would fire) and how I could just hold down the left mouse button and it will fire? Thanks!
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    pseudocode:

    Code (csharp):
    1.  
    2. float fireRate = 0.2;
    3. float fireTime = 0
    4.  
    5. Update()
    6. {
    7.     fireTime -= Time.deltaTime;
    8.  
    9.     if(attacking && fireTime <= 0)
    10.     {
    11.         fireProjectile();
    12.  
    13.         fireTime = fireRate;
    14.     }
    15. }
    16.  
     
  3. ColouredPixelsUnity

    ColouredPixelsUnity

    Joined:
    Aug 10, 2014
    Posts:
    78
    But don't you need to instantiate the bullet?
     
  4. Craig8726

    Craig8726

    Joined:
    Jul 5, 2013
    Posts:
    79
    you can use a co routine to delay the execution of firing.

    Code (CSharp):
    1. void startFiring()
    2.     {
    3.         StartCoroutine("fireBullet",0.5f);
    4.     }
    5.     IEnumerator fireBullet(float delay)
    6.     {
    7.        //instaniate bullet function
    8.         yield return new WaitForSeconds (delay);
    9.         StartCoroutine("fireBullet",delay);
    10.     }
     
  5. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,353
    Since in real life it's nearly impossible to see bullets, they're replaced with RayCast trace. For guns like rocket launcher, you'll need to instantiate projectile.

    fireProjectile() is a function which you have to define and put a shooting script into it.

    Not advising you to "make a game like pong" first, but you must at least know how the engine works and how to do C# scripting.
     
  6. BeastBomber24

    BeastBomber24

    Joined:
    Jun 10, 2019
    Posts:
    5
    Yeah but also in real life your bullet dosent make it to its location immedietly
     
  7. kielbaska_gaming

    kielbaska_gaming

    Joined:
    Mar 16, 2021
    Posts:
    1
    This will it shoot without end while you use function startFiring
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Please don't respond to six-year-old threads; it's against forum rules.

    Instead, start your own new post... it's FREE!

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand compiler and other errors and even fix them yourself:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/