Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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

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

How to shoot a bullet to the mouse cursor position? 1 Answer

Firing flame balls with animation and particles 2 Answers

Problem in Shooting Accuracy 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges