- Home /
Other
Footsteps script
Hi,
I made a script in Javscript for random footstep sounds. But it's working half. I have a float for the delay between the sounds for the normal walk speed and idem dito for sprinting. The nextFoot float does switch from the 0.525(walkspeed) to 0.3(sprintspeed) but the delay doesn't change. If I manually change the nextFoot float in the inspector, the delay does get shorter or longer, but it doesn't work when I press Left shift.
Anyone know how I can fix this?
private var isWalking : boolean = false;
private var isGrounded : boolean = false;
private var controller : CharacterController;
var nextFoot : float;
var nextFootDefault = 0.525;
var nextFootSprint : float;
var Steps : AudioClip[];
function Awake(){
controller = GetComponent(CharacterController);
}
function Update(){
if(controller.velocity.sqrMagnitude > 0.15 ){
isWalking = true;
}
else {
isWalking = false;
}
if (Input.GetKeyDown("left shift")){
nextFoot = nextFootSprint;
}
else if (Input.GetKeyUp("left shift")){
nextFoot = nextFootDefault;
}
}
InvokeRepeating("Walking", 0, nextFoot);
function Walking(){
if((isWalking) && (controller.isGrounded)){
audio.PlayOneShot(Steps[Random.Range(0,Steps.length)]);
}
}
Answer by RaptureFace · Jul 20, 2014 at 03:26 PM
I found a Great script but i don't know if it will please you here it is:
var walk : AudioClip;
var run : AudioClip;
var isWalking : boolean = false;
var isRunning : boolean = false;
function Update()
{
GetState();
PlayAudio();
}
function GetState()
{
if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ) )
{
if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
{
// Running
isWalking = false;
isRunning = true;
}
else
{
// Walking
isWalking = true;
isRunning = false;
}
}
else
{
// Stopped
isWalking = false;
isRunning = false;
}
}
function PlayAudio()
{
if ( isWalking )
{
if ( audio.clip != walk )
{
audio.Stop();
audio.clip = walk;
}
if ( !audio.isPlaying )
{
audio.Play();
}
}
else if ( isRunning )
{
if ( audio.clip != run )
{
audio.Stop();
audio.clip = run;
}
if ( !audio.isPlaying )
{
audio.Play();
}
}
else
{
audio.Stop();
}
}
(Put this in the cammera) Cheers :)
I did a modification of my script to include a sprint timer : http://answers.unity3d.com/questions/596645/limited-sprint.html