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 Braders00 · Nov 30, 2012 at 08:57 PM · timerprojectile

Delay between throwing/firing object

Hi all :D

My first question so go easy on me :P

Im quite new to scripting and I need help with the script , I want to be able to add a delay that allows a 1 or 2 second delay between throwing the snowball so that I cannot spam it.

 var projectile : Rigidbody;
 var speed = 20;
 function Update()
 {
 if( Input.GetButtonDown( "Fire1" ) )
 {
 var instantiatedProjectile : Rigidbody = Instantiate(
 projectile, transform.position, transform.rotation );
 instantiatedProjectile.velocity =
 transform.TransformDirection( Vector3( 0, 0, speed ) );
 Physics.IgnoreCollision( instantiatedProjectile. collider,
 transform.root.collider );
 }
 }

thanks :)

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
0

Answer by mononull · Nov 30, 2012 at 11:37 PM

you can use a timer. create a float variable and in the update say timer+=Time.deltaTime; if the player fired a shot. this is the time since the last update. when the timer reaches a value of 2, meaning 2 seconds have passed since the last shot reset it to 0. So when Fire1 occurs start the timer. change the if statement to (if input.getbuttondown && timer < 2). you could also count down rather than up. if you want to use a timer look at the Time class and particularly Time.deltaTime.

if i were u i would look into coroutines though. not just for this, but they are extremely useful in place of update functions that get called every loop. http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html not a lot at that link. a coroutine is like a regular function but u can hold the return of the function for so many seconds and then run some code when the time is up. you could create a coroutine like function ShotFired() and use it in conjuction with a bool. in this method instantiate the projectile and launch it or whatever. then after those statements say yield WaitForSeconds(2); then set ur bool as a sentinel meaning the player is allowed to fire again. So the bool is CanFire = true. upon firing once the bool changes to false. if(canfire == false) dont let them shoot. but once the first shot coroutine returns after 2 seconds it can switch CanFire to true...letting the player shoot again.

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

12 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

Related Questions

How do I...? [scripting help] 2 Answers

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

Simple Timer 2 Answers

Counting Down Timer But My Variable is not reaching Zero ever, Why? 1 Answer

C Sharp Messenger Extended Warning 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