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
0
Question by Reizo · Mar 18, 2016 at 12:44 PM · c#cooldown

Delayed Cooldown

How do I add delay before a cooldown starts? Here's my code for context:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerDodge : MonoBehaviour {
 
     public float dodgeCD;
     public float dodgeTimer;
 
     private Transform playerTransform;
     
     void Start () {
         playerTransform = GameObject.FindWithTag("Player").transform;
         dodgeTimer = Time.time + dodgeCD;
     }
 
     void Update()
     {
         if (Input.GetButton("Dodge") && Time.time >= dodgeTimer)
         {
             transform.position = new Vector3(playerTransform.position.x + Input.GetAxis("Horizontal") * 1.0f, 0.0f, playerTransform.position.z + Input.GetAxis("Vertical") * 1.0f);
             dodgeTimer = Time.time + dodgeCD;
         }
     }
 }

The issue I'm currently facing is that I get an instantaneous dodge (no wonder, since this code is inspired by the code for Instantiating game objects). Is there a way to make it such that once I dodge, the cooldown timer doesn't get incremented until a full second (or more) has passed?

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 Reizo · Mar 17, 2016 at 08:09 AM 0
Share

Never$$anonymous$$d, solved it myself. Here's the final code:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerDodge : $$anonymous$$onoBehaviour {
 
     public float dodgeCD;
     public float dodgeTimer;
 
     private Transform playerTransform;
     bool dodging;
     
     void Start () {
         playerTransform = GameObject.FindWithTag("Player").transform;
     }
 
     void Update()
     {
         if (Input.GetButton("Dodge") && dodgeTimer <= 0)
         {
             dodging = true;
             transform.position = new Vector3(playerTransform.position.x + Input.GetAxis("Horizontal") * 1.0f, 0.0f, playerTransform.position.z + Input.GetAxis("Vertical") * 1.0f);
         }
 
         if (dodgeTimer >= 0)
         {
             dodging = false;
             dodgeTimer = dodgeTimer - Time.deltaTime;
         }
         else if (dodgeTimer <= 0)
         {
             dodgeTimer = 0;
         }
 
         if (dodging)
         {
             StartCoroutine("dodgeCooling");
         }
     }
 
     IEnumerator dodgeCooling()
     {
         yield return new WaitForSeconds(0.1f);
         dodgeTimer = dodgeCD;
     }
 }

Inspired by this.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Flip over an object (smooth transition) 3 Answers

How do i get an ad cooldowner? 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