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 /
  • Help Room /
avatar image
0
Question by alexanderparomenskiy · Mar 22, 2019 at 06:58 AM · coroutinetimetimer

Timed button pressed C#

I'm trying to do a piece of code which times you to press certain buttons that appear on the screen, if you do not press them in time you die. However, I'm having some difficulties with Coroutines. Here is my piece of code.

 // Update is called once per frame
 void Update () {
     if (gameActive) {

         if (!R_pressed) {
             R.SetActive(true);
             // start timer
             StartCoroutine(Delay_time(R_pressed));
             // check if the key is pressed if it go to next if statement
             if (Input.GetKeyDown(KeyCode.R))
             {
                 StopCoroutine(Delay_time(R_pressed));
                 R_pressed = true;
             }
         }
         if (R_pressed && !V_pressed) {
             R.SetActive(false);
             V.SetActive(true);
             StartCoroutine(Delay_time(V_pressed));
             // check if the key is pressed if it go to next if statement
             if (Input.GetKeyDown(KeyCode.V))
             {
                 StopCoroutine(Delay_time(V_pressed));
                 V_pressed = true;
             }
         }
         if (R_pressed && V_pressed)
         {
             V.SetActive(false);
             Debug.Log("End Scene Complete");
             gameActive = false;
         }

     }
     
 }

 IEnumerator Delay_time(bool button)
 {
     yield return new WaitForSeconds(1.75f);
     if (button == false)
     {
         gameOver.SetActive(true);
     }

 }
 // checks if the player hit the collider
 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "Player" && !gameActive)
     {
         // display canvas
         CanvasObject.SetActive(true);
         gameActive = true;
     }
 }
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
1

Answer by fafase · Mar 22, 2019 at 07:03 AM

Let's break it down, when the character enters a collider, you want the player to press R within a certain time. then V within a certain time. You already have the triggering so we'll focus on the pressing issue.

We will do without Update.

  // checks if the player hit the collider
  void OnTriggerEnter(Collider other)
  {
      if (other.tag == "Player" && !gameActive)
      {
          // display canvas
          CanvasObject.SetActive(true);
         StartCorountine(CheckPress());
      }
  }
 
 IEnumerator CheckPress()
 {
      float timer = 1.75f;
      bool success = false;
      while( success == false && timer > 0f )
      {
             timer -= Time.deltaTime; // reduce timer 
             success = Input.GetKeyDown(KeyCode.R);
             yield return null;
      }
      if(success == false) 
      {
             Debug.Log("Lost");
             yield break;
      }
      success = false;
      timer = 1.75f;
      while( success == false && timer > 0f )
      {
             timer -= Time.deltaTime; // reduce timer 
             success = Input.GetKeyDown(KeyCode.V);
             yield return null;
      }
      if(success == false) 
      {
             Debug.Log("Lost");
             yield break;
      }
     Debug.Log("Won");
 }

The code repeats itself so it could benefit some extra methods to group repetition together. I kept it simple so it is easy to understand.

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
avatar image dan_wipf · Mar 22, 2019 at 07:54 AM 0
Share

nice answer+1

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

177 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Need coroutine to render a texture value clamped from 0 to 1 over time 1 Answer

Time Management. Morning, noon and night. 2 Answers

Counting Days Using In-Game Time System? 1 Answer

how can i take time value from timer (Asked again clearly) 2 Answers

Make an Object Stop Moving Upon Collision 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