- Home /
 
How to delay a shot.
Hi I need to know how to delay a shot. For example I have a game where my player has a gun and I want it so when he holds down the left mouse button the shots fire automatically, with a delay so its not shooting a hundred bullets a second. Heres the code that makes the gun shoot. This code is in java, and if you can please make a public variable so I can change the delay time. The name of the gameobject is laser which you will probably notice in the code. This code does work, but it just needs a delay, and if your wondering I don't want the if statement to go from Input.GetButton to Input.GetButtonUp or Down. I know that that will make it so the player just has to click it and it will fire, but thats not what I want it to do. I want it the same, but just a delay on when the bullets fire when I hold down the left mouse button. Thanks so much.
var throwSound : AudioClip; var coconutObject : Rigidbody; var throwForce : float;
function Update () {
if(Input.GetButton("Fire1")){ audio.PlayOneShot(throwSound); var newCoconut : Rigidbody = Instantiate(coconutObject, transform.position, transform.rotation); newCoconut.rigidbody.velocity = transform.TransformDirection(Vector3(0,0, throwForce)); newCoconut.name = "laser"; Physics.IgnoreCollision(transform.root.collider, newCoconut.collider, true);
} }
@script RequireComponent(AudioSource)
Answer by Maddogc · Jul 12, 2012 at 04:51 PM
Here you are, Unity's solution examples in JS and C# to solve your problem:
JS
 // Instantiates a projectile off every 0.5 seconds,
 // if the Fire1 button (default is ctrl) is pressed.
 var projectile : GameObject;
 var fireRate = 0.5;
 private var nextFire = 0.0;
 function Update ()
 {
 if (Input.GetButton ("Fire1") && Time.time > nextFire) 
 {
     nextFire = Time.time + fireRate;
     var clone = Instantiate (projectile, transform.position, transform.rotation);
 }
 }
 
               C#
 using UnityEngine;
 using System.Collections;
 
 public class example : MonoBehaviour 
 {
  public GameObject projectile;
  public float fireRate = 0.5F;
  private float nextFire = 0.0F;
 
  void Update() 
  {
     if (Input.GetButton("Fire1") && Time.time > nextFire) 
     {
         nextFire = Time.time + fireRate;
         GameObject clone = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
     }
  }
 }
 
              If you are satisfied that $$anonymous$$addogc has answered your question please uses the accept button
Answer by alishka · Jan 26, 2016 at 09:08 AM
Hey! Use a coroutine!
  public void OnClick () {
       if ( canShoot ) {
           StartCoroutine ( shoot() );
       }
 }
  public IEnumerator shoot () {
       //Instantiate your projectile
       shootLogic();
       canShoot = false;
       //wait for some time
       yield return new waitForSeconds (cooldownTime);
       canShoot  = true;
 }
 
               Cheers!
Answer by tjbrunetto · Aug 25, 2018 at 04:16 PM
Overall the coroutine is better. The use of the update function on a shot timer produces varying results and inconsistencies. The enumerator works perfectly.
Answer by LAKSHAYMAVIA · Sep 13, 2018 at 07:43 AM
I tried with coroutine. but unable to get Desired result.
   void startFire()
     {
       
         if (isFiring)
         {
               Debug.Log("firung");
 
             StartCoroutine(fireCouroutine());
 
             //obj.transform.parent = transform;
         }
     }
 
     IEnumerator fireCouroutine()
     {
         Vector3 offset = new Vector2(1, 0);
         GameObject obj = Instantiate(fire, transform.position + offset, Quaternion.identity);
         obj.GetComponent<SpriteRenderer>().color = Color.yellow;
         obj.GetComponent<Rigidbody2D>().velocity = new Vector2(10, 0);
 
         isFiring = false;
         Debug.Log("1" +isFiring);
         yield return new WaitForSeconds(shootDelay);
        
         isFiring = true;
         Debug.Log("2" + isFiring);
 
     }
 
               I am calling this frunction here.
 if (Input.GetKey(KeyCode.Space) || RightHalf.Contains(touchPos))
         {   
             Debug.Log("fire");
             isFiring = true;
             
              startFire();
            // InvokeRepeating("startFire", 0.003f, 0.4f);
            
             //
         }
 
               Am I doing something wrong.?
Your answer
 
             Follow this Question
Related Questions
How to delay lines of code. 2 Answers
Using Scripts in AssetBundles 0 Answers
Moveing my camera 1 Answer
Refering to gameobject script is attached to 1 Answer
Where to attach a script? 1 Answer