- Home /
Decreasing footstep length when key pressed
Hi everyone, I've being working on a horror game and I've found a "Footstep" script witch creates sounds when I'm walking (W,D,S,A Keys) and I've managed to create a Sprint function when pressing the Shift button but there's one thing that doesn't really match together: When I sprint with my Footstep Script, my footstep length remains the same. What I'm saying is when I press the Shift key, I want my sound length to be decreased witch means, increasing my speed of the footstep sound.
So here's my script of the Footstep function:
var audioStepLength = 0.3;
var footAudio : AudioClip[];
function OnTriggerEnter (other : Collider) {
var chao : System.Net.Sockets.SendPacketsElement = other.GetComponent(System.Net.Sockets.SendPacketsElement);
if(chao){
footAudio = chao.footsteps;
}
}
function PlayStepSounds () {
var controller : CharacterController = GetComponent(CharacterController);
while (true) {
if (controller.isGrounded && controller.velocity.magnitude > 0.3) {
var volume = Mathf.Clamp01(Time.time);
audio.PlayOneShot(footAudio[Random.Range(0, footAudio.Length)], volume);
yield WaitForSeconds(audioStepLength);
} else {
yield;
}
}
}
function Awake(){
PlayStepSounds();
}
If you guys have a great way to solve my question, please post them below! I would appreciate it :)
Thanks!
~crusherxman
Answer by dorpeleg · May 02, 2013 at 11:43 PM
You will need to change the audioStepLength from your sprint script.
To start of, you will need a reference to your footsteps script.
You do that by using GetComponent link
Then you need to change audioStepLength to a lower number when running and back to 0.3 when walking.
I've tried it but my footsteps length just wont work correctly (Duplicates footstep's sound)
$$anonymous$$aybe could I show you my script? (Default, not edited)
Sure, if you show your script it will be easier to help.
There you go:
var walkSpeed: float = 7; // regular speed
var crchSpeed: float = 3; // crouching speed
var runSpeed: float = 20; // run speed
private var ch$$anonymous$$otor: Character$$anonymous$$otor;
private var ch: CharacterController;
private var tr: Transform;
private var height: float; // initial height
function Start(){
ch$$anonymous$$otor = GetComponent(Character$$anonymous$$otor);
tr = transform;
ch = GetComponent(CharacterController);
height = ch.height;
}
function Update(){
var h = height;
var speed = walkSpeed;
if (ch.isGrounded && Input.Get$$anonymous$$ey("left shift") || Input.Get$$anonymous$$ey("right shift")){
speed = runSpeed;
}
if (Input.Get$$anonymous$$ey("left ctrl")){ // press C to crouch
h = 0.5 * height;
speed = crchSpeed; // slow down when crouching
}
ch$$anonymous$$otor.movement.maxForwardSpeed = speed; // set max speed
var lastHeight = ch.height; // crouch/stand up smoothly
ch.height = $$anonymous$$athf.Lerp(ch.height, h, 5*Time.deltaTime);
tr.position.y += (ch.height-lastHeight)/2; // fix vertical position
}
Oh and I've also added a "Crouch" function, just to re$$anonymous$$d you.
Your answer
Follow this Question
Related Questions
Array exists, but won't give length??? 1 Answer
How to use option setting Vibrate and Mute 1 Answer
Energy Pickup 1 Answer
Explosion Jumpscare Script ? 1 Answer
Detect if out of trigger 2 Answers