- Home /
weapon sound effect (sword)
ok i`m not great at this scripting lol i`ve spent few hours now searching the forums and net but to no avail.. lol what i`m needing is to be able to play a sound when a sword swings... i.e when i click the right mouse button, the sword swings (that wrks fine!!) but i cant get it so that everytime i click the button then the sound plays... seriously need some help with this one...
The scripting reference will help you more times than not. Here: http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.PlayOneShot.html
Answer by IndieScapeGames · Aug 10, 2012 at 08:33 PM
Add an audio source to the sword, and use this short script:
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class example : MonoBehaviour {
public AudioClip swing;
void Update(){
if (Input.GetMouseButtonDown(1)) {
Debug.Log("Pressed Right Mouse Button.");
audio.PlayOneShot(swing);
}
}
}
Answer by blackbird · Jul 13, 2017 at 05:45 PM
this has solved half of my problem my second problem when i attack the same clip keep reapting like a bad glitch when i try to do a second attack any help guys
here is my code :
using UnityEngine; using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class macheta : MonoBehaviour { public AudioClip slash; void Update(){ if (Input.GetButton("Fire1")) {
GetComponent<AudioSource>().PlayOneShot(slash);
GetComponent<AudioSource>().Stop ();
}
}
}
Your answer
Follow this Question
Related Questions
How to give the knife or sword to the human. 1 Answer
How to make an animation play always when walking in any direction? 2 Answers
Basic sword swing combat 1 Answer
play sound when kill enemy 1 Answer
Triggering different sounds for "footsteps" on different surfaces for a worm-like character. 2 Answers