Search Unity

Question Detect a range of variables for 3 seconds

Discussion in 'Scripting' started by Deleted User, Aug 27, 2021.

  1. Deleted User

    Deleted User

    Guest

    How can I detect a range of variables for 3 seconds?
     
    Last edited by a moderator: Jul 25, 2022
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Please use code tags.

    You could just sum up the delta times when the condition is true. Something like:
    Code (csharp):
    1.  
    2. private float timesum;
    3. private void Update()
    4. {
    5.     if (Data <1000 && Data > 800)
    6.     {
    7.         timesum += Time.deltaTime;
    8.     }
    9.     else
    10.     {
    11.         timesum = 0.0f;    // reset the timer when condition is no longer true
    12.     }
    13.     if(timesum >= 3.0f)
    14.     {
    15.         // your logic here
    16.     }
    17. }
    Edit: Messed up.
     
  3. Deleted User

    Deleted User

    Guest



    Can I use this code without the void update? because I need to use 'private void ReadValue' to read the correct values, but I think that if I do not use the update this code will not work.
     
  4. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    Update is called by Unity once per frame. Your ReadValue method must also be called by something in a certain intervall. If it is also called every frame (deltaTime is the time since last frame) it should work there too. If you'd post the whole script we could advise better ;).
     
  5. Deleted User

    Deleted User

    Guest

    I've made some changes and I tried to add your code to mine, but it doesn't work, probably I made some errors.
     
    Last edited by a moderator: Jul 25, 2022
  6. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    This problem description is pretty useless in programming. Try to be more specific IN WHICH WAY it doesn't work. Does it compile? Are there errors/exceptions? does it behave differently than expected? is nothing happing?

    Also I suggested to post the whole script. Who or what is calling your method? Put some Debug.Logs to see wether it is called at all. Let it print some important values. Check which logical path the program takes (if/else).
     
  7. Deleted User

    Deleted User

    Guest

    My script reads the values and reacts accordingly, but if I add your piece of code it stops working, I no longer have a reaction if it enters that range of values, nothing happen.
     
  8. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    I don't understand the meaning of that for loop. Your first code listing shows a certain condition, now you want to check a condition for many items in an array. And I don't know what this array holds. So to me it is pretty unclear what you want to achieve. You don't give proper explanation and you don't show the whole script (like I asked you multiple times). So I don't know what to base help or suggestions on. I could just guess wildly but that feels pretty useless.

    If you have problems/difficulties creating the logic for your algorithm maybe a Flow chart could be of help. It is programming language agnostic and allows to visualize and "define" algorithms offline which can then be transferred into real program code.

    Programming means to tell the computer what it has to do. But for that you must be aware yoursef what it shall do. This knowledge must then just be encoded into the code.

    Are you aware that your loop is iterated in one go? There is no noticeable break between the different elements.

    So I'm sorry but I feel a bit helpless here because I don't know how to help with that little information given. And I also don't know what your knowledge/experience is. If you are a complete beginner maybe try some tutorials first. Or learn C# programming outside of Unity.