- Home /
Improved Footsteps system
I have this Code for the footsteps
#pragma strict
var walkSpeed : float = 7;
var crouchSpeed : float = 3;
var sprintSpeed : float = 13;
static var Crouch = false;
var isCrouching = false;
var isWalking = false;
var isRunning = false;
var isIdle = true;
static var isRunning2 = false;
static var isCrouching2 = false;
static var isWalking2 = true;
var speedTest : float = 0;
var footSteps : AudioClip;
var footCool : float = 0.6;
var footTimer : float = 0.0;
private var charMotor : CharacterMotor;
private var charController : CharacterController;
private var theTransform : Transform;
private var charHeight : float;
function Start ()
{
charMotor = GetComponent(CharacterMotor);
theTransform = transform;
charController = GetComponent(CharacterController);
charHeight = charController.height;
}
function Update ()
{
audio.volume = 1;
isWalking = false;
isRunning = false;
isCrouching = false;
isIdle = true;
if(isWalking == true)
{
isWalking2 = true;
isCrouching2 = false;
isRunning2 = false;
}
if(isRunning == true)
{
isCrouching2 = false;
isWalking2 = false;
isRunning2 = true;
}
if(isCrouching == true)
{
isCrouching2 = true;
isWalking2 = false;
isRunning2 = false;
}
var h = charHeight;
var speed = walkSpeed;
Crouch = false;
if(charController.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift"))
{
speed = sprintSpeed;
isRunning = true;
isWalking = false;
isCrouching = false;
isIdle = false;
}
if(Input.GetKey("left alt") || Input.GetKey("right alt"))
{
h = charHeight*0.5;
speed = crouchSpeed;
isRunning = false;
isWalking = false;
isCrouching = true;
isIdle = false;
}
if(Input.GetKey(KeyCode.W) && isRunning == false && isCrouching == false)
{
isWalking = true;
isCrouching = false;
isRunning = false;
isIdle = false;
}
charMotor.movement.maxForwardSpeed = speed;
var lastHeight = charController.height;
charController.height = Mathf.Lerp(charController.height, h, 5*Time.deltaTime);
theTransform.position.y += (charController.height-lastHeight)/2;
if(isWalking == true && Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
{
footCool = 0.6;
stepAudio();
}
if(isRunning == true && Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
{
footCool = 0.4;
stepAudio();
}
if(isCrouching == true && Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
{
footCool = 0.75;
stepAudio();
}
if(footTimer > 0)
{
footTimer -= Time.deltaTime;
}
if(footTimer < 0)
{
footTimer = 0;
}
}
function stepAudio()
{
if(footTimer == 0)
{
audio.PlayOneShot(footSteps);
footTimer = footCool;
}
}
but the problem is that everyfootstep is exactly the same as the last one like a clock that just does tik tik tik but i want to do it that every second footstep is different so its more real like tik tok tik tok any help is apreciated and maybe other improvements for this script would be awesome too because the footsteps are terrible skullbeats1
Comment
Best Answer
Answer by zeman97 · Oct 01, 2014 at 01:20 AM
You could rewrite your stepAudio function as such:
var isRightFoot : boolean;
var rightFootSound : AudioClip;
var leftFootSound : AudioClip;
function stepAudio()
{
if(isRightFoot)
{
audio.PlayOneShot(rightFootSound);
isRightFoot = false;
}
else
{
audio.PlayOneShot(leftFootSound);
isRightFoot = true;
}
}
The function keeps track of whether or not to use the left or right foot AudioClip and then sets the isRightFoot variable to the opposite value at the end to allow for alternating.