Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 acclogin71 · May 12 at 03:41 PM · updateeventdelegateaction

is there any easier way to do this ?

this is the problem when character is idle, i want to start coroutine once, and when user presses any button, i want to stop the coroutine once. problem is code to check if user is in idle or moving is in update function.

this is my solution

 private void Update()
     {
         CheckIdle();
     }

 private bool MoveCheck, TimerCheck;
     private void CheckIdle()
     {
         MoveCheck = Input.GetAxis("Vertical") == 0 && Input.GetAxis("Horizontal") == 0;
         if (MoveCheck) // character is in idle
         {
             if (!TimerCheck) // timer has not started yet
             {
                 TimerCheck = true; // timer started
                 StartCoroutine(Idling());
             }
         }
         if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0)
         {
             if (TimerCheck) // timer is on
             {
                 TimerCheck = false; // stop timer
                 StopCoroutine(Idling());
             }
         }
     }


this works well but code looks messy. is there any simpler way to do it? can event and delegate help me in this scenario ? if yes, how ?

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 acclogin71 · May 13 at 07:44 AM 0
Share

@Hellium you are the only guy i turn to when no one else help. hope im not bothering you.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by sharatachary · May 14 at 02:44 PM

Hello @acclogin71 ,

Please have a look at this IdleMonitor implementation, hope this helps.

 /* author   : Sharat Achary
  * date     : 20220514
  */
 
 using System.Collections;
 using UnityEngine;
 
 public class IdleMonitor : MonoBehaviour
 {
     private bool isIdling;
 
     private void Start()
     {
         StartIdling();
     }
 
     private void Update()
     {
         if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
         {
             // Cancle any invoke.
             CancelInvoke("StartIdling");
 
             if (isIdling)
             {
                 StopIdling();
             }
         }
 
         if (Input.GetKeyUp(KeyCode.W) || Input.GetKeyUp(KeyCode.A) || Input.GetKeyUp(KeyCode.S) || Input.GetKeyUp(KeyCode.D))
         {
             if (!isIdling)
             {
                 // Add a delay of 1f second so that we make sure during that delay any w-a-s-d key are registered.
                 Invoke("StartIdling", 1f);
             }
         }
     }
 
     private void StartIdling()
     {
         isIdling = true;
         Debug.Log("----- Start Idling Coroutine.");
         StartCoroutine("Idling");
     }
 
     private void StopIdling()
     {
         isIdling = false;
         Debug.Log("----- Stop Idling Coroutine.");
         StopCoroutine("Idling");
     }
 
     private IEnumerator Idling()
     {
         while (true)
         {
             Debug.Log("Player is idling");
             yield return new WaitForSeconds(1f);
         }
     }
 }
 
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
avatar image
0

Answer by xxmariofer · May 13 at 09:11 AM

is hard to tell without seeing the full code, if thats the full code and you are not reading the inputs somewhere else then you cant simplify your code with events or delegates

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 acclogin71 · May 13 at 08:19 PM 0
Share

all i want to do is start a timer when w-a-s-d keys are not pressed, stop the timer when any of those keys are pressed. checking if any key is pressed or not can only happen inside update function, and when any key is not pressed, i want to start the timer from update function, only once.

avatar image xxmariofer acclogin71 · May 14 at 06:07 PM 1
Share

Your code is fine, and there are no real benefits from refactoring, leaving as it is. I would simply remove the Invoke method, and instead add a delay using WaitForSeconds inside the coroutine itself. But you can't simplify much that code

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

145 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

Related Questions

When to use 'delegate', 'event' or 'Action' ? 1 Answer

"The event can only appear on the left hand side of `+=' or `-=' operator" 1 Answer

How to make Delegates/Events survive assembly reload? 3 Answers

C# Delegates, a question 1 Answer

C# event setup, no result 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