- Home /
Footsteps script not working
I want a footsteps script with stamina. I have already implemented the sound but cannot implement the stamina. If anybody could modify the code or tell me how to do this, I would be very greatful. Here is what I have done so far:
#pragma strict
@script RequireComponent( AudioSource )
var walk : AudioClip;
var run : AudioClip;
var water : AudioClip;
var walkAudioSpeed : float = 0.4;
var runAudioSpeed : float = 0.2;
private var walkAudioTimer : float = 0.0;
private var runAudioTimer : float = 0.0;
@HideInInspector
var isWalking : boolean = false;
@HideInInspector
var isRunning : boolean = false;
var walkSpeed: float = 7; // regular speed
var runSpeed: float = 20; // run speed
@HideInInspector
var inWater : boolean = false;
private var chCtrl: CharacterController;
private var chMotor: CharacterMotor;
//Stamina Vars
private var maxStamina : float = 100.0;
private var ranOut : boolean = false;
var Stamina : float = 100.0;
var staminaRespawnRate : float = 5.5;
function Start()
{
chCtrl = GetComponent(CharacterController);
chMotor = GetComponent(CharacterMotor);
}
function Update()
{
SetSpeed();
if ( chCtrl.isGrounded )
{
PlayFootsteps();
}
else
{
walkAudioTimer = 0.0;
runAudioTimer = 0.0;
}
}
function SetSpeed()
{
//Normal health regeneration
if(Stamina < 100)
{
Stamina += Time.deltaTime * staminaRespawnRate;
}
//When health reaches 100, set to maxHealth
if(Stamina > maxStamina)
{
Stamina = maxStamina;
}
var speed = walkSpeed;
if (Stamina > 0)
{
if ( chCtrl.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift") )
{
speed = runSpeed;
}
chMotor.movement.maxForwardSpeed = speed;
}
else if (Stamina <= 0)
{
//Stamina = 0;
ranOut = true;
speed = walkSpeed;
chMotor.movement.maxForwardSpeed = speed;
isWalking = true;
isRunning = false;
}
if (Stamina <= 25)
{
ranOut = false;
}
}
function PlayFootsteps()
{
if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
{
if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
{
if (Stamina > 1 && ranOut == false)
{
// Running
isWalking = false;
Stamina -= 1;
isRunning = true;
}
else
{
// Walking
isWalking = true;
isRunning = false;
}
}
else
{
// Walking
isWalking = true;
isRunning = false;
}
}
else
{
// Stopped
isWalking = false;
isRunning = false;
}
// Play Audio
if ( isWalking && inWater)
{
if ( audio.clip != water )
{
audio.Stop();
audio.clip = water;
}
//if ( !audio.isPlaying )
if ( walkAudioTimer > walkAudioSpeed )
{
audio.Stop();
audio.Play();
walkAudioTimer = 0.0;
}
}
else if ( isWalking )
{
if ( audio.clip != walk )
{
audio.Stop();
audio.clip = walk;
}
//if ( !audio.isPlaying )
if ( walkAudioTimer > walkAudioSpeed )
{
audio.Stop();
audio.Play();
walkAudioTimer = 0.0;
}
}
else if ( isRunning && inWater)
{
if ( audio.clip != water )
{
audio.Stop();
audio.clip = water;
}
//if ( !audio.isPlaying )
if ( runAudioTimer > runAudioSpeed )
{
audio.Stop();
audio.Play();
runAudioTimer = 0.0;
}
}
else if ( isRunning )
{
if ( audio.clip != run )
{
audio.Stop();
audio.clip = run;
}
//if ( !audio.isPlaying )
if ( runAudioTimer > runAudioSpeed )
{
audio.Stop();
audio.Play();
runAudioTimer = 0.0;
}
}
else
{
audio.Stop();
}
// increment timers
walkAudioTimer += Time.deltaTime;
runAudioTimer += Time.deltaTime;
}
Thanks,
Stormy102
What's the problem? Put some debug.log's in your code and test where the code gets up to.
Answer by AlucardJay · Jul 19, 2014 at 11:10 AM
I have already added a stamina timer to my script on this answer : http://answers.unity3d.com/questions/596645/limited-sprint.html
Scroll down and use the script after Edit
Your answer
![](https://koobas.hobune.stream/wayback/20220613154455im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I stop footstep sound from playing when I release 'w'? 1 Answer
Footstep Audio Check Collider Hitting Floor 1 Answer
Random Audioclip. No Repeat ?? 1 Answer
My Footstep isn't working 2 Answers
Scale messing with code 0 Answers