I need help with a sprint script im working on. I dont know how to make stamina and then when the stamina runs out you go back to normal speed and an audio file plays (breathing). Does anybody know how to do this? Here is my sprint script so far:
using UnityEngine;
using System.Collections;
public class Sprint : 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 don’t mean for this to come off the wrong way, but maybe you should try and do a simpler game until you understand how scripting works and can get a basic idea of how to do different things. I’m going to suggest a way to do it, but not going to give you the actual code so that you can work out how it works in your head. This may not be the most optimal way to do things, either.
Give your character a variable called stamina. Only sprint while the shift key is down and while the stamina is greater than zero. When you set the run speed, decrease the stamina by the set amount. Now, remember that this is going to be called every frame, so you are going to want to decrease the stamina by a set amount per second (you can easily figure out how to do this by looking at examples on transform.Translate()).
If this doesn’t help you get it then I will write some pseudocode or something, but seeing your influx of questions on many aspects of your game, I’m thinking you should start simpler and work your way up.
EDIT:
As for the audio clip, you could do something like start a Coroutine if the stamina is zero, and in that coroutine play the audio and then yield for the length of that clip (this will make sure that the audio isn’t played over and over again).
I know i ask lots of questions and have lots of forum post but i do figure (surprisingly) a good amount of things out on my own. Im just stuck at a part on a map that involves alot of things i have never had to use before (actions). Thank you for your help, i will go step by step working out all of the script by my self, if i do however fail i might contact you if thats ok, but i will try first.
EDIT:
or if i get one thing working and cant figure out another thing then i will contact you.
Yeah, you can always post back here for help or send me a PM if you would like, but give it a shot with the process I suggested and see how it turns out 
btw, a couple minutes ago i was trying to organize my project and was moving all of my scripts into a folder, i began playing the game and when i push “W,A,S, or D” my character jumps like 10 feet in the air, i have tried to move all of them out of the folder, that still didnt work, do you know of a fix?
I wouldn’t know without seeing the entire project and seeing what actually happened. Check your code and make sure it is still okay (or that stuff didn’t get messed up in the script for the FirstPersonController). Also check and make sure that your Inputs didn’t get messed up (these are a property setting you can find here).
i honestly think i messed my entire project up…of course i dont have a backup…lol…I did try disabling scripts one by one and found that charactermotor might be the cause, i think the script is messed up, but i dont know where to get the original script…
EDIT:
Lol, to find original script all i had to do was go into another project and find charactermotor, im testing to see if it works now.
EDIT: GOT IT WORKING. Now i can start on stamina script.