|
Hi I need to know how to delay a shot. For example I have a game where my player has a gun and I want it so when he holds down the left mouse button the shots fire automatically, with a delay so its not shooting a hundred bullets a second. Heres the code that makes the gun shoot. This code is in java, and if you can please make a public variable so I can change the delay time. The name of the gameobject is laser which you will probably notice in the code. This code does work, but it just needs a delay, and if your wondering I don't want the if statement to go from Input.GetButton to Input.GetButtonUp or Down. I know that that will make it so the player just has to click it and it will fire, but thats not what I want it to do. I want it the same, but just a delay on when the bullets fire when I hold down the left mouse button. Thanks so much. var throwSound : AudioClip; var coconutObject : Rigidbody; var throwForce : float; function Update () { if(Input.GetButton("Fire1")){ audio.PlayOneShot(throwSound); var newCoconut : Rigidbody = Instantiate(coconutObject, transform.position, transform.rotation); newCoconut.rigidbody.velocity = transform.TransformDirection(Vector3(0,0, throwForce)); newCoconut.name = "laser"; Physics.IgnoreCollision(transform.root.collider, newCoconut.collider, true); } } @script RequireComponent(AudioSource)
(comments are locked)
|
|
Here you are, Unity's solution examples in JS and C# to solve your problem: JS
C#
(comments are locked)
|
