• 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 /
  • Help Room /
avatar image
Question by Velkia · Jul 19, 2017 at 10:20 PM · bulletwaitforsecondsdelaygetkey

Delay between bullet shot with GetKey

Hello, I'm trying to make a shooting game in which you can keep the W key pressed to shoot endlessly. The problem is that there is absolutely no delay between each bullet, so this looks more like a strange laser than a proper shoot. I tried to use WaitForSeconds but it couldn't work... I would like to know where I'm wrong. Help would be greatly appreciated. :) Here's my code :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class playerControl : MonoBehaviour {
 
     public GameObject playerBulletGO; //bullet prefab
     public GameObject bulletPosition01;
     public GameObject bulletPosition02;
     public float speed;
 
 
         // Use this for initialization
         void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
 
         if(Input.GetKey("w") || Input.GetKey("z")) {
 
             Shoot();
             StartCoroutine(ShootWait());
 
         }
 
         
         float x = Input.GetAxisRaw ("Horizontal"); // will be -1 0 or 1 (for left, no input, and right)
         float y = Input.GetAxisRaw ("Vertical"); // will be -1 0 or1 (for down, no input and up)
 
         //now based on the input we comput a direction vector and we normalize it to get a unit vector
         Vector2 direction = new Vector2 (x, y).normalized;
 
         //now we call the function that computes and set the player's position
         Move (direction);
     }
 
     void Move(Vector2 direction)
     { //here is the code for moving
     }
 
     void Shoot()
     {
         //instantiate the 1st bullet
         GameObject bullet01 = (GameObject)Instantiate(playerBulletGO);
         bullet01.transform.position = bulletPosition01.transform.position; //set the bullet initial position
 
         //instantiate the 2nd bullet
         GameObject bullet02 = (GameObject)Instantiate(playerBulletGO);
         bullet02.transform.position = bulletPosition02.transform.position; //set the bullet initial position
     }
 
     IEnumerator ShootWait()
     {
         yield return new WaitForSeconds(100.0f);
     }
 }
 
Comment
Hellium

People who like this

-1 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 Hellium · Jul 20, 2017 at 09:57 AM 0
Share

Do you seriously believe you are the first one asking this question ? Make research please. Unity Answers is full of similar questions.

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Sopqos · Jul 20, 2017 at 11:52 AM

 public float fireRate = 2F; // 2seconds wait
 private float nextFire = 0.0F;

.

 if(Input.GetKey("w") || Input.GetKey("z")  && Time.time > nextFire ) {
 
               nextFire = Time.time + fireRate;
              Shoot();
  }

That should work (wont need startcourotine this way )

Comment
Jackal14
mllabedz

People who like this

2 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 TexusDark · Jul 20, 2017 at 06:13 PM 0
Share

Invoke() is a better choice because he use a function when he clicked so : Invoke("Shoot", 2); should be better than your method.

avatar image Sopqos TexusDark · Jul 20, 2017 at 06:19 PM 0
Share

Well, now he has 2 choices :)

avatar image

Answer by TexusDark · Jul 19, 2017 at 10:34 PM

Hi!

Use Invoke() :)

You have the documentation here : Invoke()

Comment
Hellium

People who like this

-1 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

111 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

Related Questions

Delay in setting Boolean Value 1 Answer

Wait time Before Camera Switching? 0 Answers

How to put delay after input key 0 Answers

Making button function wait for function before it to finish / Using WaitForSeconds. 0 Answers

How to wait a certain amount of seconds in C# 4 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