• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 19, 2019 at 05:15 PM by Nosmo for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Nosmo · Mar 18, 2019 at 07:28 PM · mobileshootingshootfiretime.time

how do i stop automatic firing?

I've got my character firing via a mobile input button, but when i start the game the character fires once automatically. how do i stop this?

code

 public Transform firePoint;
 public GameObject bulletPrefab;

 public float fireRate; //addition
 private float nextfire; //addition

 // Update is called once per frame
 void Start()
 {
     {
         Shoot();
     }
 }

 public void Shoot()
 {    
       nextfire = Time.time + fireRate;
       Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
       Debug.Log("Bang");
       //Instantiate (flash, flashSpawn.position, flashSpawn.rotation); //addition

      //    GunShotSound.Play (); //addition
 }

}

addition:

That fire rate is unchanged no matter what I set it to, any suggestions as to why this is?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mchts · Mar 18, 2019 at 07:51 PM 0
Share

but when i start the game the character fires once automatically

If you call Shoot(); in Start it shoots when game starts :) just remove that line. And where else do you use fireRate because there is no way to understand whether it works or not just these lines of code.

3 Replies

  • Sort: 
avatar image
0

Answer by Tsaras · Mar 18, 2019 at 07:46 PM

You need to understand a little more about coding in general and for unity in specific. What your code does right now is calling the Start function which is called once on the first frame the object is active which goes through the Shoot() function which shoots one bullet exactly. You haven't actually implemented any system to use input from user or firing at a specific rate.

You need to give us more information about how you want the triggering of the firing to occur, but as for the firing itself you need to implement a delayed firing system which actually uses the fire rate variable. Something like this:

  public Transform firePoint;
  public GameObject bulletPrefab;
  public float fireRate; //addition
  private float nextfire; //addition
  
  void Update()
  {
     if (Time.time >= nextfire){
         Shoot();
     }
  }
  
  public void Shoot()
  {    
        nextfire = Time.time + fireRate;
        Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
        Debug.Log("Bang");
        //Instantiate (flash, flashSpawn.position, flashSpawn.rotation); //addition
       //    GunShotSound.Play (); //addition
  }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by xxmariofer · Mar 18, 2019 at 07:53 PM

remove the Start method completly

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Nosmo · Mar 18, 2019 at 08:12 PM

I solved auto fire myself. I re-added;

if (Input.GetButtonDown("Fire1") && Time.time > nextfire)

to start and made start public and connected the fire button to it. Thanks for your help anyway.

As for fire rate, I don't use it in any other script and the only other script associated with firing is attached to the bullet and that covers speed, what it's hit and destroying it after 4 frames

 public float speed = 20f;
 public Rigidbody rb;

 // Use this for initialization
 void Start()
 {
    rb.velocity = transform.forward * speed;
 }

 void OnTriggerEnter (Collider hitInfo)
 {
    Debug.Log(hitInfo.name);
 }

 // Update is called once per frame
 void Update()
 {
     Destroy(gameObject, 4.0f);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

154 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Firing System with two Shoot Points 1 Answer

Hold Button Shooting 3 Answers

Shoot when GUI is pressed? 0 Answers

Help with fire script 1 Answer

How can i set up a turret that turns and shoots where you touch onscreen?,how to shoot where you touch 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges