- Home /
How do I put a delay on a gunshot?
How do I put a delay on a gunshot script I have an if statement playing audio shooting a bullet and showing a one shot particle instance.
Answer by GamezAtWork · Nov 10, 2010 at 01:04 AM
Perhaps try using the Invoke function? Or yield...
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.Invoke.html Regarding the Invoke function
http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html Explaining yield
Answer by Jaap Kreijkamp · Nov 10, 2010 at 03:58 AM
Use coroutines! In C# you would get something like:
public AudioClip bulletSound; bool isFiring = false;
void Update() { // ... if (Input.GetButton("Fire1") && !isFiring) { StartCoroutine(COFire()); } // ... }
IEnumerator COFire() { isFiring = true; yield return new WaitForSeconds(0.1f); // trigger delay // create bullet and flash effect... yield return 0; // wait 1 frame // stop flash yield return new WaitForSeconds(0.05f); // wait 0.05seconds AudioSource.PlayClipAtPoint(bulletSound, transform.position); yield return new WaitForSeconds(0.1f); // extra delay before you can shoot again isFiring = false; }
Your answer
Follow this Question
Related Questions
Need help with bullet/gun scripting 3 Answers
A few android questions. 0 Answers
Animation & Script Help 2 Answers
NullReferenceException on my gunscript 1 Answer
Camera Recoil,Please help me. 2 Answers