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
1
Question by AusAndrew19 · Oct 11, 2012 at 07:29 AM · teleportcooldown

How to make a cooldown on a script

Hello People, I Have a script for a pretty nice teleport but the issue I'm having is that there is no cool down so if i can repeatedly press teleport and it will.. I Would like to add a 10second cool down for this spell but I'm afraid i do not have the knowledge on adding a cool down or what system is needed to achieve this. Here is the script so people can see it and hopefully help me out on this issue. Thank you!

 var teleportSound: AudioClip; // assign the sound in the Inspector
 var distance: float = 20;
 
 function Update(){
   if (Input.GetKeyDown("t")){
     var dest = transform.position + transform.forward * distance;
     var hit: RaycastHit;
     // if some obstacle between player and dest...
     if (Physics.Linecast(transform.position, dest, hit)){
       // set dest to 1.5m before the obstacle
       dest = transform.position + transform.forward * (hit.distance - 1.5); 
     }
     dest.y = 1000; // choose a really high spot to do the raycast
     if (Physics.Raycast(dest, -Vector3.up, hit)){ // if there's ground below the destination point...
       dest = hit.point; // define the destination somewhat above the hit.point
       dest.y += 2;      // to make the character fall to the ground
       transform.position = dest;
       audio.PlayOneShot(teleportSound);
     }
   }
 }

So if anyone could give me some information on this or maybe point me in the correct direction i would be more then appreciative. Thank you for you time reading this. :)

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 Fattie · Oct 11, 2012 at 08:17 AM 0
Share

you just need the Invoke() command. it's the simplest way to do timers and the like in Unity.

so it would be something like

 var disableTeleportAtThe$$anonymous$$oment:boolean

 teleport function would look like this...
 if ( disableTeleportAtThe$$anonymous$$oment ) return;
 teleport code
 disableTeleportAtThe$$anonymous$$oment = true
 Invoke( "teleportO$$anonymous$$Again", 10.0 );

 function teleportO$$anonymous$$Again()
 disableTeleportAtThe$$anonymous$$oment = false;

ok ?

1 Reply

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

Answer by well_played_mike · Oct 11, 2012 at 08:22 AM

You can add one more condition in your if statement, so it's something like this :

 var nextTeleport;
 var duration = 2.0;
 
 if (Input.GetKeyDown("t") &&  Time.time > nextTeleport){
     nextTeleport = Time.time + duration;
     var dest = transform.position + transform.forward * distance;


For more information please look into Time.time documentation.

Comment
Add comment · Show 1 · 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

11 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

Related Questions

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

how to add a sound to this script 3 Answers

Teleport enemy after player collides with an object? 0 Answers

How to toggle a key for a car to go forward or backward? 1 Answer

Error whit c# code 'UnityEngine.Transform.position' because it is not a variable. 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