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 /
avatar image
0
Question by squall_789 · Aug 30, 2010 at 04:47 PM · delayshootfirewait

Slowing down the speed of my bullets whilst holding in the key.

Hey guys.

I'm quite the n00b in Unity but im slowly getting more comfortable with it. I've since a few hours ago, run into a small problem, well 2 problems actually, but they are both practically identical, so the answers from this should help me fix the 2nd problem.

Problem:

when I use (Input.GetKey("z")) to fire my weapon, my bullets fire out like crazy, I understand this to be because the bullets fire with each update call, which is every frame. However I'm trying to find a way to have the bullets STOP firing for a second before firing again, so its not as much as a constant stream of bullets.

My code, is as follows, this is an expansion of the 3DBUZZ video series, so that code is used as my base.

Blockquote using UnityEngine; using System.Collections;

public class Player : MonoBehaviour {

// Use this for initialization

public float PlayerSpeed; public GameObject ProjectilePrefab;

// Update is called once per frame void Update () { float amountToMoveHorizontal = Input.GetAxisRaw("Horizontal") PlayerSpeed Time.deltaTime; transform.Translate(Vector3.right * amountToMoveHorizontal);

 float amountToMoveVertical = Input.GetAxisRaw("Vertical") * PlayerSpeed * Time.deltaTime;
 transform.Translate(Vector3.up * amountToMoveVertical);

 //if (Input.GetKey("z"))
 if (Input.GetKey("z"))
 {
     //Fire Projectile on the Left & Right (Note...Eventually make into a single command...)

     Vector3 leftposition = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
     Instantiate(ProjectilePrefab, leftposition, transform.rotation);

     Vector3 rightposition = new Vector3(transform.position.x + transform.localScale.x * 8, transform.position.y + transform.localScale.y * 5);
     Instantiate(ProjectilePrefab, rightposition, transform.rotation);

     Vector3 right2position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
     Instantiate(ProjectilePrefab, right2position, Quaternion.Euler(0, 0, 20));

     Vector3 right3position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
     Instantiate(ProjectilePrefab, right3position, Quaternion.Euler(0, 0, 40));

     Vector3 right4position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
     Instantiate(ProjectilePrefab, right4position, Quaternion.Euler(0, 0, 60));

     Vector3 right5position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
     Instantiate(ProjectilePrefab, right5position, Quaternion.Euler(0, 0, 70));

     Vector3 right6position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
     Instantiate(ProjectilePrefab, right6position, Quaternion.Euler(0, 0, 80));

     Vector3 right7position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
     Instantiate(ProjectilePrefab, right7position, Quaternion.Euler(0, 0, 85));

     Vector3 right8position = new Vector3(transform.position.x + transform.localScale.x * -8, transform.position.y + transform.localScale.y * 5);
     Instantiate(ProjectilePrefab, right8position, Quaternion.Euler(0, 0, 90));

 }



}

} > Blockquote

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

1 Reply

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

Answer by Daniel-Brauer · Aug 30, 2010 at 05:08 PM

The easiest way to do this is to keep track of when the last bullet was fired:

float firingRate = 0.1f; //delay between shots, in seconds float lastFired = -100f; //absolute time of last fired shot

void Update() { //early out if we have fired recently if (Time.time < lastFired + firingRate) { return; }

 //if we can fire, we record the new lastFired time
 lastFired = Time.time;

 //and actually fire the bullet
 //... firing code here

}

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 squall_789 · Aug 30, 2010 at 05:24 PM 0
Share

Thanks man, that worked like a Charm. Works perfectly now. However Could you please example quite what "lastFired" does, changing it to any value appears to make no difference, however changing it to 100 ins$$anonymous$$d of -100 causes the bullets to not fire at all. Thanks once again.

avatar image Daniel-Brauer · Aug 30, 2010 at 05:42 PM 0
Share

lastFired is just the last time a bullet was fired. If it started at zero, you wouldn't be able to fire for a moment, until firingRate seconds had passed. When you set it to 100, you can't fire until the game has played for more than 100 seconds. I should actually have made firingRate public, as it is a value you might want to tweak.

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

No one has followed this question yet.

Related Questions

Shoot Bullet Delay Enemy 0 Answers

How could I shoot a chemical in a fire extinguisher? 1 Answer

Aim at Something with Delay 1 Answer

How to WaitforSeconds a GuiButton ? 0 Answers

how do I shoot where my cross hair points? 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