Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Need help adding a timer of some sort to this

Discussion in 'Scripting' started by ImNexuss, Aug 31, 2023.

  1. ImNexuss

    ImNexuss

    Joined:
    Mar 6, 2013
    Posts:
    4
    I have basically no knowledge of programming but can wrap my head around it when I take my time....I'm curious if anyone could tell me if it would be possible to esentially add a timer here related to (data.isSprinting) and crouching/ (data.state == 1)

    Basically I want to make it so that if you switch from sprinting (data.isSprinting) to crouching (data.state == 1), after a set amount of time, the state will automatically switch back crouching....

    I've scrambled but I'm wondering if anyone could make it work without me posting the entire script here....Here's the important portion...

    Code (CSharp):
    1.       //Take rotation in consideration (local to world)
    2.                         data.moveDirection = pb.transform.TransformDirection(data.moveDirection);
    3.                         //Apply speed based on state
    4.                         //Standing
    5.                         if (data.state == 0)
    6.                         {
    7.                             //Sprinting
    8.                             if (data.isSprinting)
    9.                             {
    10.                                 data.moveDirection *= sprintSpeed;
    11.                             }
    12.                             //Not sprinting
    13.                             else
    14.                             {
    15.                                 data.moveDirection *= walkSpeed;
    16.                             }
    17.                         }
    18.                         //Crouching
    19.                         else if (data.state == 1)
    20.                         {
    21.                             data.moveDirection *= crouchSpeed;
    22.                         }
    23.  
    24.                         if (!pb.cc.isGrounded)
    25.                         {
    26.                             data.moveDirection.y = yMovement;
    27.                         }
    28.                         else
    29.                         {
    30.                             //Mouse Look multiplier
    31.                             data.moveDirection *= pb.looking.GetSpeedMultiplier(pb);
    32.                             //Weapon multiplier
    33.                             data.moveDirection *= pb.weaponManager.CurrentMovementMultiplier(pb); //Retrive from weapon manager
    34.                                                                                                   //Should play slow animation?
    35.                             data.playSlowWalkAnimation = pb.weaponManager.IsAiming(pb); //Retrive from weapon manager
    36.                         }
    37.                     }
     
  2. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    A simple timer would just be:
    Code (CSharp):
    1. public class SomeClass : MonoBehaviour
    2. {
    3.     int timer;
    4.     int timer2;
    5.  
    6.     void Update()
    7.     {
    8.         timer++;
    9.         if (timer < 60)
    10.         {
    11.             // stuff...
    12.         }
    13.         else
    14.         {
    15.             timer2++;
    16.            
    17.             // stuff
    18.            
    19.             if (timer2 > 60)
    20.             {
    21.                 // end stuff
    22.                 // reset timers
    23.                 timer = 0;
    24.                 timer2 = 0;
    25.             }
    26.         }
    27.     }
    28. }
    Which is a simple example ^. But mostly you want timers to go by "frames per second", so they actually line up with how fast the game runs, especially if you want it to be real seconds(or minutes) of time.

    So it's really on you to determine what you want to wait on, before something stops doing something, and reverts back to what it was doing originally.

    But personally I have a variable "fps"(homemade) and I have my int timers check against that, to calculate how long to wait:
    if (timer > fps * 2) = if 2 seconds passed
     
  3. ImNexuss

    ImNexuss

    Joined:
    Mar 6, 2013
    Posts:
    4
    How could I implement this into the above portion of the script though? I really appreciate the explanation, but it almost seems like what you're saying would require an entirely seperate script? I just want to make it so that if the state is sprinting, then changes to crouching, there is a timer activitated that changed the state back to crouching.... not entirely sure how i could add what you explained in between what I explained....Would pasting the entire script im working with be more helpful?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Assuming you have some sort of state machine present or some way to know when the state has changed, then when you enter the sprinting state, you just need to:
    • Make note of whether the previous state was crouching
    • Start ticking up a timer with each update of the state machine
    • If the user deigns to stop sprinting within the threshold, go back to crouching
    Looking at your brief bit of code, it does look like you need to be recording the current state, and checking when the current state is changed.
     
  5. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    Not at all. A more particular answer in your case, say you wanted crouch to end after 5 secs:
    Code (CSharp):
    1. //Crouching
    2. else if (data.state == 1)
    3. {
    4.     timerCrouch++;
    5.     data.moveDirection *= crouchSpeed;
    6.     if (timerCrouch > fps * 5.0f) { data.state = 0; timerCrouch = 0;}
    7. }
    However with simple timers you also need a way to zero-out the specific timer, as per say you only crouched for 4 seconds, then left off this section, that timer would hit next time with only one second left, unless it was reset elsewhere.

    But once you get accustomed to writing them, it's no different than clearing a reference by making it null. I particularly find them useful for debugging as well.
     
  6. ImNexuss

    ImNexuss

    Joined:
    Mar 6, 2013
    Posts:
    4
    I'll literally toss someone a small amount of money to properly implement this....I think I understand the second above comment, but I'm getting tones of erros....As I said, I really have no serious knowledge of programming, but it makes sense to me logically....I'd be more than happy to pay someone a small amount to hop on disc or something similar to help me out. I'll paste the entire script and maybe if someones feeling generous they can try to piece it together, but otherwise, someone just toss me contact information...
     
  7. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    That won't work because you have data.state == 0 for sprinting and data.state == 1 for crouching. The trick to solve this kind of issues is to use a binary mask, for example idle == 0, walking == 1, sprinting == 2, crouching == 4, grounded == 8. So, if your character is sprinting and crouching then you get 4+2 == 6.

    As for the time, since you are in Update(), you just need to increment a variable with Time.deltaTime until you're done.

    Here's a simplified version:

    Code (csharp):
    1.  
    2.    [SerializedField] private float sprintLimit = 1.0f;
    3.     private float crouchSprint = 0.0f;
    4.  
    Code (csharp):
    1.  
    2.  
    3.     if (state == SPRINTING | CROUCHING)
    4.     {
    5.         if (crouchSprint >= sprintLimit)
    6.         {
    7.              crouchSprint = 0.0f; // reset
    8.              state = CROUCHING;
    9.         }
    10.         else {
    11.              crouchSprint += Time.deltaTime;
    12.         }
    13.     }
    14.  
     
  8. ImNexuss

    ImNexuss

    Joined:
    Mar 6, 2013
    Posts:
    4
    if you want to help with this being implented, I can pay you....Lmk, primarily communication is discord
     
  9. dlorre

    dlorre

    Joined:
    Apr 12, 2020
    Posts:
    700
    Sorry I won't have time for this but you can ask this in the dedicated forum for hiring or on some site like Upwork.
     
  10. wideeyenow_unity

    wideeyenow_unity

    Joined:
    Oct 7, 2020
    Posts:
    728
    If you typed it out word for word, then yes you'd get errors. You'd need to declare an int of timer(
    int timerCrouch;
    ), like I showed in my first post. That was just showing you where things go, it wasn't a separate class.

    And "fps" would also give you an error if you typed it out, as you haven't declared that either. Without knowing what frames this is running at, I wasn't gonna assume "60fps", because if your running 120+ frames you'd probably not even notice the wait time.

    So my response was vague, I apologize, but it's hard to assume, since code is so exact. :)

    But just declare the int timer at the top of your class, and find what frames you run at, and exchange that number for the "fps" part, and it should be all good.

    But just keep working at it, the trials and tribulations you face when trying to understand code, stick with you forever once you figure it out. Although it's quite the "headache" to figure things out, it's only a short pain, then a life long achievement of always remembering it, once it all makes sense.
     
  11. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    521
    You should also use this opportunity to simplify your code. It is a fairly good example to use.

    You see this part? what is different is only the "speed" but it is always multiplying by whatever speed is appropriate so move those calculations into a method that returns the speed. Probably best to pass the data object to it and it can figure out which speed to return.

    Code (CSharp):
    1. if (data.state == 0)
    2. {
    3.     //Sprinting
    4.     if (data.isSprinting)
    5.     {
    6.         data.moveDirection *= sprintSpeed;
    7.     }
    8.     //Not sprinting
    9.     else
    10.     {
    11.         data.moveDirection *= walkSpeed;
    12.     }
    13. }
    14. //Crouching
    15. else if (data.state == 1)
    16. {
    17.     data.moveDirection *= crouchSpeed;
    18. }
    19.  
    And your usage becomes:

    data.moveDirection *= GetSpeedMultiplier(data);