- Home /
The question is answered, right answer was accepted
FootSteps Script
Well, I have this script:
var audioStepLength: float = 0.1; var footAudio: AudioClip[];
var controller: CharacterController;
function Awake(){ FootStepsSound(); }
function FootStepsSound(){ while(true){ if (controller.isGrounded && controller.velocity.magnitude > 0.5){ var volume = Mathf.Clamp01(Time.time); controller.audio.PlayOneShot(footAudio[Random.Range(0, footAudio.Length)], volume); yield WaitForSeconds(audioStepLength); } else { yield; } } }
I attach it to a Blank GameObject, and set the Controller to my Player, and attach an AudioSource to my Player, but, when I click Play, the Unity stop working.
I know this, can you give me an script? Here is an algorithm:
If Player.Velocity > 0.5 Do: Sound.Play(FootStep); Wait(2s); Repeat If
Answer by williampigmeu · Aug 01, 2012 at 09:04 PM
Oh, sorry, I just got it working, if someone wants it, it needs 2 scripts made by me:
RunSneak (attach to Player):
/*** * RunSneak * Make character run and sneak * * Author: WilliamPigmeu */
// Interacting with BobbingView for a realistic head-bob
static var bobbingSpeed; static var bobbingAmount; static var playerStamina = 250;
static var audioStepLength: float = 0.5; static var stepSound = 0;
var walkSpeed: float = 7; // regular speed var sneakSpeed: float = 3; // sneaking speed var runSpeed: float = 20; // running speed
var walkStep: float = 0.5; var sneakStep: float = 1; var runStep: float = 0.45;
private var isRunning: boolean = false;
private var chMotor: CharacterMotor; private var tr: Transform; private var dist: float; // distance to ground
function Start(){ chMotor = GetComponent(CharacterMotor); tr = transform; var ch:CharacterController = GetComponent(CharacterController); dist = ch.height/2; // calculate distance to ground }
function Update(){ if(chMotor.grounded){ bobbingSpeed = 0.20; bobbingAmount = 0.3; } else { bobbingSpeed = 0; bobbingAmount = 0; }
var vScale = 1.0;
var speed = walkSpeed;
audioStepLength = walkStep;
stepSound = 2;
if (!isRunning){ if (playerStamina < 250){ playerStamina = playerStamina+2; } } else if (isRunning){ if (playerStamina >= 0){ playerStamina = playerStamina-3; } }
isRunning = false;
if (Input.GetKey("left shift") && chMotor.grounded){ // press LShift to run
if (playerStamina >= 3){ isRunning = true;
audioStepLength = runStep; stepSound = 0;
speed = runSpeed; bobbingSpeed = 0.30; bobbingAmount = 0.3; } else { isRunning = false; } }
if (Input.GetKey("c")){ // press C to sneak
speed = sneakSpeed;
bobbingSpeed = 0.15;
bobbingAmount = 0.1;
audioStepLength = sneakStep;
stepSound = 1;
}
chMotor.movement.maxForwardSpeed = speed; // set max speed
var ultScale = tr.localScale.y; // crouch/stand up smoothly
tr.localScale.y = Mathf.Lerp(tr.localScale.y, vScale, 5*Time.deltaTime);
tr.position.y += dist * (tr.localScale.y-ultScale); // fix vertical position
}
FootSteps (attach to anything and set controller to Player):
var footAudio: AudioClip[]; var controller: CharacterController;
private var RunSneak: RunSneak = null;
function Awake(){ FootStepsSound(); }
function FootStepsSound(){ while(true){ if (controller.isGrounded && controller.velocity.magnitude > 3){ var volume = Mathf.Clamp01(Time.time); controller.audio.PlayOneShot(footAudio[RunSneak.stepSound], volume); yield WaitForSeconds(RunSneak.audioStepLength); } else { yield; } } }
Answer by DNP · Aug 01, 2012 at 08:40 PM
If the while is making it crash, add this right after while
yield WaitForSeconds (1);
Answer by soutroll · Oct 29, 2013 at 06:13 PM
#pragma strict
@script RequireComponent( AudioSource )
var walk : AudioClip;
var run : AudioClip;
var walkAudioSpeed : float = 0.4;
var runAudioSpeed : float = 0.2;
private var walkAudioTimer : float = 0.0;
private var runAudioTimer : float = 0.0;
var isWalking : boolean = false;
var isRunning : boolean = false;
var walkSpeed: float = 7; // regular speed
var runSpeed: float = 20; // run speed
private var chCtrl: CharacterController;
private var chMotor: CharacterMotor;
function Start()
{
chCtrl = GetComponent(CharacterController);
chMotor = GetComponent(CharacterMotor);
}
function Update()
{
SetSpeed();
if ( chCtrl.isGrounded )
{
PlayFootsteps();
}
else
{
walkAudioTimer = 0.0;
runAudioTimer = 0.0;
}
}
function SetSpeed()
{
var speed = walkSpeed;
if ( chCtrl.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift") )
{
speed = runSpeed;
}
chMotor.movement.maxForwardSpeed = speed;
}
function PlayFootsteps()
{
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;
}
// Play Audio
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 )
{
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 for posting my script without giving any credit or any reference to where you got it from .....
http://answers.unity3d.com/questions/373508/footsteps-script-for-running-and-walking.html
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Scripts not on instantiated GameObjects? 1 Answer
animating character 1 Answer
iTween(JS version) could not be used in Unity iPhone? 3 Answers
Very simple inventory script... 0 Answers