- Home /
Walking sound Help
I am currently trying to make a walking sound for my game. i get the sound to play using audio.Play () but it rapidly plays then i tried audio.PlayOneShot()and it kept spawning the same sound(but it did make cool dubstep) The audio plays when it detects me walking and i was wondering if there is a function, or string i could use that would limit how many times it plays the sound every time im walking
Answer by syclamoth · Jan 31, 2012 at 05:47 AM
If you have some kind of signal for when you start and stop walking, you can use this-
// when you start walking audio.Play();
// when you stop walking audio.Stop();
I can't really tell you how to make something like that unless I know how your walk script works, though.
EDIT: You're supposed to edit your question, not post an answer. Seriosuly, in what way is that an answer?
In any case, you should do it just after you collect your inputs-
if(!audio.isPlaying && horizontalMovement.magnitude > 0)
{
audio.Play();
}
if(audio.isPlaying && horizontalMovement.magnitude == 0)
{
audio.Stop();
}
Answer by MP2fps · Jan 31, 2012 at 07:02 AM
var walkAcceleration : float = 5; var walkAccelAirRacio : float = 0.1; var walkDeacceleration : float = 5; @HideInInspector var walkDeaccelerationVolx : float; @HideInInspector var walkDeaccelerationVolz : float;
var cameraObject : GameObject; var maxWalkSpeed : float = 20; var horizontalMovement : Vector2;
var jumpVelocity : float = 20; @HideInInspector var grounded : boolean = false; var maxSlope : float =60; var WalkSound : AudioClip; //^the above variable is not in this script function Update() {
horizontalMovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
if (horizontalMovement.magnitude > maxWalkSpeed)
{
horizontalMovement= horizontalMovement.normalized;
horizontalMovement *= maxWalkSpeed;
animation.Play();
}
rigidbody.velocity.x = horizontalMovement.x;
rigidbody.velocity.z = horizontalMovement.y;
if (grounded){
rigidbody.velocity.x = Mathf.SmoothDamp(rigidbody.velocity.x, 0, walkDeaccelerationVolx, walkDeacceleration);
rigidbody.velocity.z = Mathf.SmoothDamp(rigidbody.velocity.z, 0, walkDeaccelerationVolz, walkDeacceleration);
}
transform.rotation = Quaternion.Euler(0,cameraObject.GetComponent(MouseLookScript).currentYRotation, 0);
if(grounded)
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal")* walkAcceleration * Time.deltaTime, 0, Input.GetAxis("Vertical")* walkAcceleration * Time.deltaTime);
else
rigidbody.AddRelativeForce(Input.GetAxis("Horizontal")* walkAcceleration * walkAccelAirRacio * Time.deltaTime, 0, Input.GetAxis("Vertical")* walkAcceleration * walkAccelAirRacio * Time.deltaTime);
if(Input.GetButtonDown("Jump")&& grounded)
rigidbody.AddForce(0,jumpVelocity,0);
}
function OnCollisionStay (collision : Collision) { for (var contact : ContactPoint in collision.contacts) { if (Vector3.Angle(contact.normal, Vector3.up)< maxSlope) grounded = true; }
} function OnCollisionExit () { grounded = false;
Sorry i'm new to unity answers, any way i got it to play when i walk and stop playing when i stop walking but now it just rapidly plays when i walk