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 john 9 · May 12, 2011 at 08:58 PM · waitforsecondsdelaybullets

yield WaitForTime(1); question

where would i place the wait for seconds ion this script so that only 1 bullit fires per second?

var OnCollideExplode : Transform; var shootForce:float; function Update() { if(Input.GetButtonDown("Throw")) { var instanceBullet=Instantiate(OnCollideExplode, transform.position, Quaternion.identity); instanceBullet.rigidbody.AddForce(transform.forward * shootForce); }

}

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 oliver-jones · May 12, 2011 at 09:08 PM 0
Share

You can't put a yeildWaitForSeconds in an update function because an update function is called every frame

3 Replies

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

Answer by Eric5h5 · May 12, 2011 at 09:17 PM

Use a coroutine instead of Update.

function Start () {
    while (true) {
        if (Input.GetButton("Throw")) {
            Throw();
            yield WaitForSeconds(1.0);
        }
        yield;
    }
}
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 Molix · May 12, 2011 at 11:50 PM 0
Share

This is neat. I'm curious though, since this is still called each frame, what is the difference internally and performance-wise (if there is any) between this and using Update? i.e. could/should you do this for everything and never use Update?

avatar image Eric5h5 · May 13, 2011 at 01:01 AM 0
Share

@$$anonymous$$olix: It's only called every frame while waiting for input, but once the throw button is pressed, it waits for a second, during which time nothing at all happens (except whatever internal method is used to wait for 1.0 seconds, of course). So if you press the throw button a lot, then performance would be slightly better, since it wouldn't have to run an Update function and associated code every frame. Overall it's more about using code that's easier to write and maintain. Update is still useful, but for code that involves various ti$$anonymous$$gs, coroutines usually make more sense.

avatar image
0

Answer by Bryan 4 · May 12, 2011 at 09:09 PM

add a timer. function Update()

{ if(Input.GetButtonDown("Throw") && Time.time - fireTimer >= 1.0f) { var instanceBullet=Instantiate(OnCollideExplode, transform.position, Quaternion.identity); instanceBullet.rigidbody.AddForce(transform.forward * shootForce); fireTimer = Time.time; }

}

this will make it so you can only fire once per second. (change the 1.0f) to whatever if you want different timings.

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 DaveA · May 12, 2011 at 09:11 PM 0
Share

Ya beat me by less than a $$anonymous$$ute, well done

avatar image Bryan 4 · May 12, 2011 at 09:16 PM 0
Share

I'm so happy those typing lessons finally paid off.

avatar image
0

Answer by DaveA · May 12, 2011 at 09:10 PM

var OnCollideExplode : Transform; var shootForce:float; var lastShootTime = 0;

function Update() { if (Input.GetButtonDown("Throw")) { if ((Time.time - lastShootTime) > 1) { var instanceBullet=Instantiate(OnCollideExplode, transform.position, Quaternion.identity); instanceBullet.rigidbody.AddForce(transform.forward * shootForce); lastShootTime = Time.time; } }

}

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 john 9 · May 13, 2011 at 07:27 AM 0
Share

Thanks guys for this but i wasn't really clear i don't think basicly i need absolute decimal point precision ti$$anonymous$$g on this any idea?

avatar image DaveA · May 13, 2011 at 02:36 PM 0
Share

1.0, 1.00? Update is called for every frame and unless you're machine is really old, it should be like 60 or more times per second, so it should be within .01 or .02 of that mark. You could look into the $$anonymous$$SDN for better times. OnGUI is called several times per frame, so it might have more precision that way.

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

How to delay function Update execution? 2 Answers

30 Objects all firing at exactly the same time and not randomly 1 Answer

Can IEnumerator be called more than once? 1 Answer

How to create a delay in a function being called in update? 2 Answers

yield WaitForSeconds to delay the instantiation of a game Object; 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