Search Unity

Shots on loop

Discussion in 'Getting Started' started by KarBis, Nov 11, 2019.

  1. KarBis

    KarBis

    Joined:
    Sep 6, 2019
    Posts:
    25
    Hi guys,

    I have this script attached to the boss, but it's shooting only when I press mouse, and I would like to put it in the loop to shoot 2times per second, how could I do it?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class EnemiesShots : MonoBehaviour
    6. {
    7.     public float speed;
    8.  
    9.     public GameObject shot;
    10.     public Transform shotSpawn;
    11.     public float fireRate;
    12.  
    13.     private float nextFire;
    14.  
    15.     void Update ()
    16.     {
    17.         if (Input.GetButton("Fire1") && Time.time > nextFire)
    18.         {
    19.             nextFire = Time.time + fireRate;
    20.             Instantiate(shot, shotSpawn.position, shotSpawn.rotation);
    21.         }
    22.     }
    23.  
    24. }
     
  2. KarBis

    KarBis

    Joined:
    Sep 6, 2019
    Posts:
    25
    I found out, If I remove "Input.GetButton("Fire1") &&" it works :)
     
    JoeStrout likes this.