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 /
avatar image
1
Question by Adam Bruns · Feb 20, 2010 at 12:06 AM · timeyielddelayweapon

How to implement a delay between each time my gun fires (using coroutines or otherwise)?...

So I've taken some parts from the FPS tutorial script and chopped them up for my use. I'm right now only testing a sniper, and it will kill the "monster" i have, it shoots as fast as a machine gun, I dont know whats going on, everything here seems right:

var damage = 20; var clips = 20; var reloadTime = 2; private var hitParticles : ParticleEmitter;

function Start () { hitParticles = GetComponentInChildren(ParticleEmitter);

 // We don't want to emit particles all the time, only when we hit something.
 if (hitParticles)
     hitParticles.emit = false;

}

function Fire () { var direction = transform.TransformDirection(Vector3.forward); var hit : RaycastHit;

 // Did we hit anything?
 if (Physics.Raycast (transform.position, direction, hit)) {
             if (hitParticles) {
         hitParticles.transform.position = hit.point;
         hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
         hitParticles.Emit();
     }

     // Send a damage message to the hit object          
     hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
 }


 Reload();

}

function Reload(){ yield WaitForSeconds(reloadTime); clips--; }

I get no errors in the debug box and everything should work, but it still shoots like a machine gun. I cant figure it out, maybe you can

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

2 Replies

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

Answer by duck · Apr 06, 2010 at 11:57 AM

because you're calling "Reload()", and Reload() is a coroutine, your coroutine starts running separately to your main "Fire" function, and your "Fire" function is free to continue executing at full speed.

If you want your firing to repeat, with a certain delay between every shot, while the fire button is held down, you could use something like this:

function Start() { // start up the shooting timer coroutine: ShootTimer(); }

function ShootTimer() { // a repeating test to see whether the user is pressing fire while (true) { if (Input.GetButton ("Fire1")) { Fire(); yield WaitForSeconds(reloadTime); } else { yield; } } }

function Fire() { // perform one shot (raycast, particles, etc), // but no reload timing goes in here }


Alternatively, you could use the more "traditional" approach instead of coroutines (which can sometimes be simpler to understand), where you measure the amount of time elapsed since the last shot yourself:

var reloadTime = 0.5; private var nextFireTime = 0.0;

function Update () { if (Input.GetButton ("Fire1") && Time.time > nextFire) { nextFireTime = Time.time + fireRate; Fire(); } }

Comment
Add comment · Show 3 · 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 Adam Bruns · Apr 07, 2010 at 08:33 PM 0
Share

Thanks that should work Duck, and sorry Sebas for not responding, i kinda forgot i had this question up haha

avatar image Sebas · Apr 07, 2010 at 09:03 PM 0
Share

no worries, glad it got solved :)

avatar image Adam Bruns · Apr 07, 2010 at 09:15 PM 0
Share

yeah, also what i forgot to say is that its called by another script and has a Send$$anonymous$$essageUpwards("Fire") thingy on it. Dunno why i didnt post that in the question

avatar image
0

Answer by Sebas · Feb 20, 2010 at 02:15 AM

Just a guess:

You didn't post your Update() function or the function where you actually fire your weapon (where you call your Fire() function. By looking at the above code, could it be that you can even fire without any ammunition? Possibly add an if statement that you can only fire when a bullet is loaded. What you have up there is simply the WaitForSeconds() to reload. Your described behavior would result if you continuously fire your gun without ammunition and the reloading just happens without any consequence.

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

No one has followed this question yet.

Related Questions

Delaying through yield 2 Answers

"Script error: Update() can not be a coroutine.", please help 1 Answer

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

How do you delay Application.LoadLevel ? 2 Answers

How to delay a function (how to use WaitForSeconds() and yield) 1 Answer


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