- Home /
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;
}
}
in unity for very simple timers, just use Invoke()
it is incredibly simple
So it would be like
Invoke( "StopRunning", 5.0 );
and perhaps
Invoke( "HardAgain", 15.0 );
you just write the two routines StopRunning() etc which would be extremely simple (no more than one or two lines of code each)
there is vast discussion of this on unityGE$$anonymous$$S.com
Answer by clunk47 · Dec 14, 2012 at 09:58 PM
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 :)
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();
}
}
}
the code has worked, it brings up the option of duration time however my player seems to still run forever. Any ideas why?
I have edited the script above and added audio, and it will play when your player is "out of breath" or out of running energy. I have included 2 different ways of adding an Audio Source component which are [RequireComponent(typeof(AudioSource))], and by checking for an audiosource in void Start(), and adding one if there is not one. Then I set the clip to the public clip you drag onto the script. Don't worry about dragging a clip on the AudioSource Component in the inspector. Just drag your breathing sound onto the "Out Of Breath" option on this script in the inspector. I've tested and this works, use whatever sound you want. For best results, click on your sound clip in the inspector and set it to 2D sound (uncheck the 3D Sound box).
It works like a charm! Thanks again for your wonderful help. However i must ask, is there a volume control for the sound effect? right now its louder than the background music
Yes Sir! You notice when I say PlayOneShot(audio.clip, 1.0f)... The 1.0f is the volume. Volume ranges as a float in script from 0.0f to 1.0f (1.0f being the loudest.) Try using 0.5f or so :)
I thank you once again :) I know this is completely off topic from the question but, may i ask how did you learn scripting? and do you thing its possible to learn it independently from books and videos?
Your answer
Follow this Question
Related Questions
Play audio from directory? 0 Answers
useing WWW Class 1 Answer
Distribute terrain in zones 3 Answers
[C#]Toggle Run on/off 2 Answers
Multiple Cars not working 1 Answer