**Hi!**
I have a problem with my footsteps script when I walk down some stairs ingame. Just watch the video below to see what happens.
**Video:**
https://www.youtube.com/watch?v=fHtyNNV0ELA
Does anyone know why it's like that?
**Footstep script:**
public var walkSounds : AudioClip[];
public var runSounds : AudioClip[];
public var walkAudioSpeed : float = 0.4;
public 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;
public var walkSpeed : float = 8;
public var runSpeed : float = 20;
public var sprintDuration : float = 10.0;
private var sprintTimer : float = 0;
private var stamina : float = 0.0;
var CamAnimationScript : CamAnimation;
//public var staminaTexture : Texture;
private var chCtrl: CharacterController;
private var chMotor: CharacterMotor;
public var flashlightAnimation : GameObject;
function Start()
{
chCtrl = GetComponent(CharacterController);
chMotor = GetComponent(CharacterMotor);
}
function Update()
{
SetSpeed();
if ( chCtrl.isGrounded )
{
PlayFootsteps();
}
else
{
walkAudioTimer = 1000.0;
runAudioTimer = 1000.0;
}
}
function SetSpeed()
{
var speed = walkSpeed;
if ( Input.GetAxis( "Horizontal" ) || Input.GetAxis( "Vertical" ))
{
if ( Input.GetKey( "left shift" ) || Input.GetKey( "right shift" ) )
{
// Running
isWalking = false;
isRunning = true;
CamAnimationScript.running = true;
CamAnimationScript.walking = false;
}
else
{
// Walking
isWalking = true;
isRunning = false;
flashlightAnimation.animation.CrossFade ("Flashlight", 0.2);
CamAnimationScript.running = false;
CamAnimationScript.walking = true;
}
}
else
{
// Stopped
isWalking = false;
isRunning = false;
flashlightAnimation.animation.CrossFade ("Flashlight", 0.2);
}
// check the sprint timer
if ( isRunning )
{
sprintTimer += Time.deltaTime;
if ( sprintTimer > sprintDuration )
{
isRunning = false;
isWalking = true;
CamAnimationScript.running = false;
CamAnimationScript.walking = true;
}
else if ( chCtrl.isGrounded )
{
speed = runSpeed;
}
}
else
{
sprintTimer -= Time.deltaTime;
}
sprintTimer = Mathf.Clamp( sprintTimer, 0, sprintDuration );
chMotor.movement.maxForwardSpeed = speed;
stamina = 1.0 - ( sprintTimer / sprintDuration );
}
function PlayFootsteps()
{
// Play Audio
if ( isWalking )
{
if ( walkAudioTimer > walkAudioSpeed )
{
audio.Stop();
audio.clip = walkSounds[ Random.Range( 0, walkSounds.Length ) ];
audio.Play();
walkAudioTimer = 0.0;
flashlightAnimation.animation.CrossFade ("Flashlight", 0.2);
}
}
else if ( isRunning )
{
if ( runAudioTimer > runAudioSpeed )
{
audio.Stop();
audio.clip = runSounds[ Random.Range( 0, runSounds.Length ) ];
audio.Play();
runAudioTimer = 0.0;
flashlightAnimation.animation.CrossFade ("FlashlightSprint", 0.2);
}
}
else
{
audio.Stop();
}
// increment timers
walkAudioTimer += Time.deltaTime;
runAudioTimer += Time.deltaTime;
}
//function OnGUI()
//{
// if ( staminaTexture ) // check there is a texture so it doesn't error if not
// {
// GUI.BeginGroup(new Rect(30, Screen.height - 50, 405 * stamina, 25));
// GUI.DrawTexture(new Rect(0, 0, 405, 25), staminaTexture);
// GUI.depth = 0;
// GUI.EndGroup();
// }
//}
↧