x


Limiting the time my gun can shoot

I am trying to limit the rate of fire for my gun. I use this script:

function Update () 
{
    machineflash = GameObject.FindWithTag("machineflash");
    // raycast settings
    var direction = transform.TransformDirection(Vector3.forward);
    var hit : RaycastHit;
    var localOffset = transform.position;

    // check to see if user has clicked
    if(Input.GetButton("Fire1"))
    {
       {
       // if so, did we hit anything?
       if (Physics.Raycast (localOffset, direction, hit, 400)) 
       {
         // just so we can see the raycast
         Debug.DrawLine (localOffset, hit.point, Color.cyan);
         // print to console to see if we have fired
         print("we have fired!");
         // send damage report to object
         hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
       }
       AudioSource.PlayClipAtPoint(gunshot,transform.position);
       Instantiate (muzzleflash, machineflash.transform.position, transform.rotation);
       }
}

But whenever I try to put a WaitForSeconds, it says that function Update() cannot be a coroutine. How can I fix this.

more ▼

asked May 27 '11 at 07:53 AM

Max 4 gravatar image

Max 4
254 60 64 71

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You can use a boolean to prevent fire and set it outside of Update in a coroutine.

if(Input.GetButton("Fire1") && !gunFired)
{
   StartCoroutine(GunFire());
}

use this outside of Update

function GunFire()
{
   gunFired = true;
   yield WaitForSeconds(1);
   gunFired = false;
}
more ▼

answered May 27 '11 at 07:57 AM

homeros gravatar image

homeros
321 1 2 6

Assets/Player/machinegun.js(16,41): BCE0005: Unknown identifier: 'gunFired'.

May 27 '11 at 08:07 AM Max 4

var gunFired : bool = false;

use this on top of your code as a field.

May 27 '11 at 08:16 AM homeros

Where do I put the first part? It doesn't seem to work.

May 27 '11 at 08:22 AM Max 4

I don't exactly know which one you meant by first part but i'll list everything.

-put "var gunFired : bool = false;" on top of your code, outside of every function.

-replace "if(Input.GetButton("Fire1"))" with "if(Input.GetButton("Fire1") && !gunFired)"

-put "StartCoroutine(GunFire());" on the first line inside if statement

-put function GunFire() { gunFired = true; yield WaitForSeconds(1); gunFired = false; }

anywhere outside of Update function.

May 27 '11 at 08:27 AM homeros

Thanks mate, works like a charm. It was more complicated than anything I could have figured out.

May 27 '11 at 08:44 AM Max 4
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x588
x175
x137

asked: May 27 '11 at 07:53 AM

Seen: 829 times

Last Updated: May 27 '11 at 09:21 AM