- Home /
My Running sound doesn't play or sound as it should, Help
Hello, I've been reading how to add footsteps sound and sounds in general to the game and think I've got about the right idea but there are certain sound that don't play as they should, This is the script for a 2d like Castlevania or Dust Elysian Tale.
if (Input.GetKey(KeyCode.RightArrow) && !Playing_Left && Milu_Grounded && !Ground_First_Press && Skip_Next_Movement && !Milu_Airborne && !Facing_Left){
Vector3 right = transform.TransformDirection(Vector3.right);
if (rigidbody.velocity.x < (Movement_Speed_Walk/3.7f) && !Milu.IsPlaying("Crouch")){
rigidbody.AddForce(right * (Movement_Speed_Walk/3.7f) , ForceMode.Impulse);
}
if (!Milu.IsPlaying("Walk")) {
Milu.Play("Walk");
Walking_Milu = true;
Idle_Milu = false;
Playing_Right = true;
Facing_Left = false;
Crouching_Milu = false;
Milu_Landing = false;
Space_Bar_Hold = false;
Running_Milu = false;
Skip_Next_Movement = true;
Time_Lapse_Walk = Time.time; // Esta linea crea un margen de tiempo entre el tiempo real y el "Time_Lapse_Walk"
// para revisar si se esta dentro de este margen de tiempo y se detecte el double-tap
if(Walking_Milu && Milu_Grounded){
audio.volume = 0.4f;
audio.clip = Walking_HardFloor_Sound;
audio.Play();
audio.loop = true;
}
}
}
/* Este pedazo de aqui invierte el "!Ground_First_Press && Skip_Next_Movement" a "Ground_First_Press && !Skip_Next_Movement"
* cuando se suelta la techa de la flecha derecha, para que cuando se vuelva a presionar la flecha se revisen y se pueda correr*/
if (Input.GetKeyUp(KeyCode.RightArrow) && Facing_Left == false){
Skip_Next_Movement = false;
Ground_First_Press = true;
Walking_Milu = false;
Milu_Landing = false;
//audio.Stop();
}
/* Aqui ocurre practicamente lo mismo que en Walk exceptuando que esto se ejecuta si se esta dentro del margen de tiempo
* previamente creado y "!Ground_First_Press && Skip_Next_Movement" estas invertidas, se aplica una fuerza con otros valores
* y se corre la animacion "Run"*/
if (Input.GetKey(KeyCode.RightArrow) && ((Time.time - Time_Lapse_Walk) < Time_Lapse_Run && Ground_First_Press && !Skip_Next_Movement && !Playing_Left && !Milu_Airborne) || (Running_Milu && !Playing_Left && !Milu_Airborne)){
Vector3 right = transform.TransformDirection(Vector3.right);
if (rigidbody.velocity.x < (Movement_Speed_Walk*0.9f) && !Milu.IsPlaying("Crouch")){
rigidbody.AddForce(right * (Movement_Speed_Walk*0.9f) , ForceMode.Impulse);
}
Milu.Play("Run");
Walking_Milu = false;
Idle_Milu = false;
Facing_Left = false;
Playing_Right = true;
Crouching_Milu = false;
Milu_Landing = false;
Space_Bar_Hold = false;
Running_Milu = true;
Ground_First_Press = true;
Skip_Next_Movement = false;
Time_Lapse_Walk = Time.time;
if(Running_Milu && Milu_Grounded){
audio.clip = Running_HardFloor_Sound;
audio.Play();
audio.loop = true;
}
}
// Comments are in spanish, my language so I dont forget what I did there
It starts playing like it should, the walking sounds plays but when the character runs it plays some sort of static while its grounded, yet if I jump while running the sound plays correctly, I really dont understand why.
Thanks.
Answer by RaptureFace · Jul 20, 2014 at 03:08 PM
It's easy Heres the script...The only Flaw is that it plays the sound while in air...
Paste the script in the new script you made...
Add it to the cam...
Put your sound and Test.
Script:
#pragma strict
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();
}
}
Enjoy.
I did a modification of my script to include a sprint timer : http://answers.unity3d.com/questions/596645/limited-sprint.html
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Footsteps? 4 Answers
Simple footsteps? 1 Answer
Sprinting Audio Problem 1 Answer
Footsteps Problem 2 Answers