Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by PlayKiseki · Oct 08, 2014 at 12:05 AM · bulletdelayenablekeypress

Why is there a delay before the script functions?

Hi there,

In my project, the player can hold down the "Fire1" key to shoot bullets in autofire mode. For some reason, however, there is about a second delay from pressing the key to the bullet appearing. Could somebody please take a look at my code and help me understand why this is happening?

Script 1 - Gets the bullet from a pool:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class playergunscript : MonoBehaviour
 {
         public float fireRate = 4.0f;
         private float counter = 0.0f;
 
         void Start ()
         {
                 this.enabled = false;
         }
 
         void Update ()
         {
                 counter += Time.deltaTime;
 
                 if (counter > 1 / fireRate) {
                         GameObject obj = playerbulletpool.current.GetPooledObject ();
                         obj.transform.position = transform.position;
                         obj.transform.rotation = transform.rotation;
                         obj.SetActive (true);
                         counter = 0.0f;
                 }
         }
 }

Script 2 - Autofire when "Fire1" is held:

 sing UnityEngine;
 using System.Collections;
 
 public class playercontrolscript : MonoBehaviour {
 
     private playergunscript playergunscript;
 
     void Start(){
                 playergunscript = GetComponentInChildren<playergunscript> ();
         }
 
     void Update () {
                 if (Input.GetButton ("Fire1")) {
                         playergunscript.enabled = true;
 
                 } else {
                         playergunscript.enabled = false;
                 }
         }
 }
 }

Thank you very much for your time!

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Oct 08, 2014 at 01:36 AM

Your problem is this line:

   counter += Time.deltaTime;

You re enabling and disabling the playergunscript. When the gun script is disabled, you don't get Update() calls, so counter is not being incremented. Only when the "Fire" button is pressed will it increment. You have a number of choices. If it were me, I'd put a Fire() method in playergunscript and then have the script decide if it can fire or not. As an alternate, you could use a timestamp. Each time the gun fires you record the time. Then your check would be:

 if (recordedTime + (1.0f / fireRate) <= Time.time) {

This does not require Update to increment a counter.

Comment
Add comment · Show 2 · 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 Bunny83 · Oct 08, 2014 at 02:03 AM 1
Share

shouldn't be the condition somehow the other way round? Time.time is increasing and if it's greater than your timeout you will fire again.

avatar image robertbu · Oct 08, 2014 at 04:07 AM 0
Share

@Bunny83 - fixed. Thanks.

avatar image
0

Answer by BadAssGames · Oct 08, 2014 at 12:33 AM

   void Update ()
         {
                 counter += Time.deltaTime;
  
                 if (counter > 1 / fireRate) {
                         GameObject obj = playerbulletpool.current.GetPooledObject ();
                         obj.transform.position = transform.position;
                         obj.transform.rotation = transform.rotation;
                         obj.SetActive (true);
                         counter = 0.0f;
                 }
         }

The line if(counter > 1 / fireRate) { it looks like you want to wait .25 seconds (because your fireRate is 4.0). If this is so, you need to cast the 1 as a float, because right now it is an integer. So change the 1 in that line to 1.0f. Should fix the issue.

On a further note, if the purpose of counter is just to make a short delay, a coroutine with a yield statement would be a little more efficient.

Comment
Add comment · Show 1 · 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 Bunny83 · Oct 08, 2014 at 02:00 AM 0
Share

Actually the value is 0.25f because a integer divided by a float yields a float, not an integer. Only when two integers are involved the result will be an integer as well. So:

 float i = 1 / 4;  // will be 0 as both are integers
 float f = 1 / 4f; // will be 0.25f
avatar image
0

Answer by PlayKiseki · Oct 09, 2014 at 07:33 AM

Hi all,

Thanks for your answers! I understand that the delay was caused by the Update() method not being called until the button was clicked. I took @robertbu's advice and instead created an isFiring() bool which is called when the button is clicked instead and there is no longer a delay. I haven't updated the counter to a coroutine yet since right now there's no performance issues. I'll consider it when I do some code cleanup, however.

Here are the updated, working scripts (including the ability to fire left and right). I hope this question can help somebody else:

playergunscript:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class playergunscript : MonoBehaviour
 {
         public float fireRate = 4.0f;
         private float counter = 0.0f;
         public bool isFiring = false;
 
         void Update ()
         {
                 counter += Time.deltaTime;
 
                 if (isFiring == true) {
                         if (counter > 1.0f / fireRate) {
                                 GameObject obj = playerbulletpool.current.GetPooledObject ();
                                 obj.transform.position = transform.position;
                                 obj.transform.rotation = transform.rotation;
                                 obj.SetActive (true);
                                 counter = 0.0f;
                         }
                 }
         }
 }
 

playercontrolscript:

 using UnityEngine;
 using System.Collections;
 
 public class playercontrolscript : MonoBehaviour
 {
 
         public Vector3 speed = new Vector2 (10, 10);
         private Vector2 movement;
         private playergunscript playergunscript;
         private playergunleft playergunleft;
 
         void Start ()
         {
                 playergunscript = GetComponentInChildren<playergunscript> ();
                 playergunleft = GetComponentInChildren<playergunleft> ();
         }
 
         void Update ()
         {
                 // get axis information
                 float inputX = Input.GetAxis ("Horizontal");
                 float inputY = Input.GetAxis ("Vertical");
         
                 // movement per direction
                 movement = new Vector2 (
             speed.x * inputX,
             speed.x * inputY);
 
                 if (Input.GetButton ("Fire1")) {
                         playergunscript.isFiring = true;
                         playergunleft.isFiring = false;
                 } else {
                         playergunscript.isFiring = false;
                 }
                 if (Input.GetButton ("Fire2")) {
                         playergunleft.isFiring = true;
                         playergunscript.isFiring = false;
                 } else {
                         playergunleft.isFiring = false;
                 }
         }
 
         void FixedUpdate ()
         {
                 rigidbody2D.velocity = movement;
         }
 
 }
 

Thanks again for your suggestions!

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

[HELP] Rotate Bullet Script 0 Answers

Enable/Disable shader on script 2 Answers

Enter Trigger and Rigidbody Enable 1 Answer

Add Weapon 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