How can i add a time limit to running?

I have a C# script that makes my player (FPS) run and crouch but i want to add a time limit to running. so i want to make it 5 seconds running time and 15 seconds regeneration time with out of breath sound effects. How would i go about doing so?

Here’s the script i’m using

using UnityEngine;
using System.Collections;

public class RunAndCrouch : MonoBehaviour 
{
    public float walkSpeed = 7; // regular speed
    public float crchSpeed = 3; // crouching speed
    public float runSpeed = 20; // run speed

    private CharacterMotor chMotor;
    private Transform tr;
    private float dist; // distance to ground

    // Use this for initialization
    void Start () 
    {
       chMotor =  GetComponent<CharacterMotor>();
        tr = transform;
        CharacterController ch = GetComponent<CharacterController>();
        dist = ch.height/2; // calculate distance to ground
    }

    // Update is called once per frame
    void FixedUpdate ()
    {
       float vScale = 1.0f;
        float speed = walkSpeed;

        if ((Input.GetKey("left shift") || Input.GetKey("right shift")) && chMotor.grounded)
        {
            speed = runSpeed;       
        }

        if (Input.GetKey("c"))
        { // press C to crouch
            vScale = 0.5f;
            speed = crchSpeed; // slow down when crouching
        }

        chMotor.movement.maxForwardSpeed = speed; // set max speed
        float ultScale = tr.localScale.y; // crouch/stand up smoothly 

       Vector3 tmpScale = tr.localScale;
       Vector3 tmpPosition = tr.position;

       tmpScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5 * Time.deltaTime);
       tr.localScale = tmpScale;

       tmpPosition.y += dist * (tr.localScale.y - ultScale); // fix vertical position       
       tr.position = tmpPosition;
    }
}

I would add a runDuration float, a regenTime float, and use an Enumerator to be able to count the durations. I haven’t had time to test this, but I did run my scene with the script involved and no errors or warnings. This might not work exactly as you wanted but this should get you to the point where you can get this idea to work. This is more of an edit of your code with the necessary additions that you can experiment with, hope this helps :slight_smile:

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(AudioSource))]

public class RunAndCrouch : MonoBehaviour 
{
    public float walkSpeed = 7; // regular speed
    public float crchSpeed = 3; // crouching speed
    public float runSpeed = 20; // run speed
    public float runDuration = 5.0f;
    public float runRegenTime = 15.0f;

    private CharacterMotor chMotor;
    private Transform tr;
    private float dist; // distance to ground

    bool canRun = true;
    bool tryRun = false;
	
	public AudioClip outOfBreath;

    // Use this for initialization
    void Start () 
    {
		///AUDIO
		if(!GetComponent<AudioSource>())
			gameObject.AddComponent<AudioSource>();
		audio.clip = (AudioClip)outOfBreath;
		audio.loop = false;
		audio.playOnAwake = false;
		///
        StartCoroutine("CheckRunEnergy");
        chMotor =  GetComponent<CharacterMotor>();
        tr = transform;
        CharacterController ch = GetComponent<CharacterController>();
        dist = ch.height/2; // calculate distance to ground
    }

    // Update is called once per frame
    void FixedUpdate ()
    {
		print (canRun);
       float vScale = 1.0f;
        float speed = walkSpeed;

        if ((Input.GetKey("left shift") || Input.GetKey("right shift")) && chMotor.grounded)
        {
         tryRun = true;
         if(canRun)
                speed = runSpeed;
         else if(!canRun)
          speed = walkSpeed;
        }
       else
         tryRun = false;

        if (Input.GetKey("c"))
        { // press C to crouch
            vScale = 0.5f;
            speed = crchSpeed; // slow down when crouching
        }

        chMotor.movement.maxForwardSpeed = speed; // set max speed
        float ultScale = tr.localScale.y; // crouch/stand up smoothly 

       Vector3 tmpScale = tr.localScale;
       Vector3 tmpPosition = tr.position;

       tmpScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5 * Time.deltaTime);
       tr.localScale = tmpScale;

       tmpPosition.y += dist * (tr.localScale.y - ultScale); // fix vertical position       
       tr.position = tmpPosition;
    }

    IEnumerator CheckRunEnergy()
    {
       while(true)
       {
         if(tryRun)
         {
          while(canRun)
          {
              yield return new WaitForSeconds(runDuration);
              canRun = false;
			  audio.PlayOneShot(outOfBreath, 1.0f);
          }
          while(!canRun)
          {
              yield return new WaitForSeconds(runRegenTime);
              canRun = true;
          }
         }


         yield return new WaitForEndOfFrame();
       }
    }
}