Need help adding a timer of some sort to this

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…

      //Take rotation in consideration (local to world)
                        data.moveDirection = pb.transform.TransformDirection(data.moveDirection);
                        //Apply speed based on state
                        //Standing
                        if (data.state == 0)
                        {
                            //Sprinting
                            if (data.isSprinting)
                            {
                                data.moveDirection *= sprintSpeed;
                            }
                            //Not sprinting
                            else
                            {
                                data.moveDirection *= walkSpeed;
                            }
                        }
                        //Crouching
                        else if (data.state == 1)
                        {
                            data.moveDirection *= crouchSpeed;
                        }

                        if (!pb.cc.isGrounded)
                        {
                            data.moveDirection.y = yMovement;
                        }
                        else
                        {
                            //Mouse Look multiplier
                            data.moveDirection *= pb.looking.GetSpeedMultiplier(pb);
                            //Weapon multiplier
                            data.moveDirection *= pb.weaponManager.CurrentMovementMultiplier(pb); //Retrive from weapon manager
                                                                                                  //Should play slow animation?
                            data.playSlowWalkAnimation = pb.weaponManager.IsAiming(pb); //Retrive from weapon manager
                        }
                    }

A simple timer would just be:

public class SomeClass : MonoBehaviour
{
    int timer;
    int timer2;

    void Update()
    {
        timer++;
        if (timer < 60)
        {
            // stuff...
        }
        else
        {
            timer2++;
           
            // stuff
           
            if (timer2 > 60)
            {
                // end stuff
                // reset timers
                timer = 0;
                timer2 = 0;
            }
        }
    }
}

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

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?

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.

Not at all. A more particular answer in your case, say you wanted crouch to end after 5 secs:

//Crouching
else if (data.state == 1)
{
    timerCrouch++;
    data.moveDirection *= crouchSpeed;
    if (timerCrouch > fps * 5.0f) { data.state = 0; timerCrouch = 0;}
}

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.

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…

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:

   [SerializedField] private float sprintLimit = 1.0f;
    private float crouchSprint = 0.0f;
    if (state == SPRINTING | CROUCHING)
    {
        if (crouchSprint >= sprintLimit) 
        {
             crouchSprint = 0.0f; // reset
             state = CROUCHING;
        }
        else {
             crouchSprint += Time.deltaTime;
        }
    }

if you want to help with this being implented, I can pay you…Lmk, primarily communication is discord

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.

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. :slight_smile:

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.

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.

if (data.state == 0)
{
    //Sprinting
    if (data.isSprinting)
    {
        data.moveDirection *= sprintSpeed;
    }
    //Not sprinting
    else
    {
        data.moveDirection *= walkSpeed;
    }
}
//Crouching
else if (data.state == 1)
{
    data.moveDirection *= crouchSpeed;
}

And your usage becomes:

data.moveDirection *= GetSpeedMultiplier(data);