- Home /
How to use sound effects for push
Hi there guys, So basically here is my front movement script, but I am unsure of how to write up a script that would make a sound work for the push that is used in this script. I have already imported the sound to unity and made a variable of it accordingly..(var pushSound : AudioClip;) how do I proceed further to make the sound work when I push the box?
here is the coding so far..
if (Input.GetKeyDown(KeyCode.UpArrow)) {
if (Physics.Raycast (transform.position, fwd, hit,1)) {
print ("There is something in front of the object!");
if(hit.collider.gameObject.tag!="Push"
)
{
if (hit.collider.gameObject.tag == "endGame"){
Application.LoadLevel(0);
}else{
transform.position += Vector3.up;
//animation.CrossFade("DefaultAnim", 0.2);
}
}
else{
print ("Push!");
var pushHit1:RaycastHit;
if (!Physics.Raycast (hit.collider.gameObject.transform.position, fwd, pushHit1,1)) {
hit.collider.gameObject.transform.position += Vector3.forward;
transform.position += Vector3.forward;
audio.PlayOneShot(pushSound, 0.7);
}
}
}else{
Debug.Log("nothing in front");
Answer by KiraSensei · Jun 05, 2013 at 07:52 PM
Attach the component AudioSound to the object to push (with the sound to be played, of course).
Then after moving your object, play the sound :
hit.GetComponent(AudioSource).Play();
ps : I think your hit variable is a game object ? In that case the line above is good, and you can simplify your code :
hit.transform.position += Vector3.forward;
And
if(hit.tag!="Push")
Instead of
if(hit.collider.gameObject.tag!="Push")
that does simplify my code and makes it look neater.. btw.. where do I exactly put=
hit.GetComponent(AudioSource).Play();
After
hit.transform.position += Vector3.forward;
for example
Your answer
![](https://koobas.hobune.stream/wayback/20220613103606im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Advance when the sound is done playing 1 Answer
Multiple/Different footsteps sounds (solved) 2 Answers
Sound help with script 1 Answer
help with multiple sounds 0 Answers
can foe hear us? 1 Answer