Controlled Automatic Fire Rate (RayCasting)

Hey guys. I am having some problems with an automatic but controlled ray cast firing system. It goes as followed;

if (Input.GetButton("Fire1") && aiming && canShoot && !shooting && currentClip >= 1 && !reloading && m9showing && Time.time > nextFire)
	{
		Shooting.shooting();
   		currentClip -= 1;
		shooting = true;
		canShoot = false;
		animation.Play("Z_Multiple_Shot");
	}
	if(Input.GetButton("Fire1") && walking && !aiming && m9showing && Time.time > nextFire || Input.GetButton("Fire1") && idle && !aiming && currentClip >= 1 && !reloading && m9showing && Time.time > nextFire)
	{
		Shooting.shooting();
   		currentClip -= 1;
		shooting = true;
		canShoot = false;
		animation.Play("Multiple_Shot");
	}

//----------------------------------------------------------------------------------------------------------\

#pragma strict

var bulletHole : GameObject;

var TheDammage : int = 100;

function Update()
{
   			Debug.DrawRay (transform.position, Vector3.forward * 10, Color.green);
}

function shooting ()
{	
	var hit : RaycastHit;
	for(var i : int = 0; i < 10; i++)
	{
		if(Physics.Raycast(transform.position, transform.forward, hit,100.0))
		{
   			var hitRotation = Quaternion.FromToRotation(Vector2.up, hit.normal);
   	 		if(hit.transform.tag == "Wall")
   			{
   				Instantiate(bulletHole, hit.point, hitRotation);
 	 		}
   	 		hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
   			Debug.Log( " Ray Hit " + hit.collider.gameObject.name);

		}
	yield WaitForSeconds(.08);
	}	
}

I understand that when if(Input.GetButton("Fire1")) where as if(Input.GetButtonDown("Fire1")) will only fire once per click. On another note my shooting(); class is connected to another game object but I know I am accessing the script’s class correctly. I was thinking a timer but I could’t figure out how to imply it. Does ray casting itself have a function that will control how much or fast it will ray cast? Anyways I need to slow down the fire rate using a timer but I don’t know how to do this. Thanks in advance!

You fire as many rays as buttons you click. Am i correct in assuming you want to create an automatic weapon? If so, try this:

float fireRate = 0.15f;
float lastShot = -10.0f;

if(Time.time > fireRate + lastShot)//.time is an intrinsic variable. 
{
//raycast goes here
//damage enemy health
lastShot = Time.time;//this must be and the end of the if(Time.time > fireRate + lastShot)
}

Also, Input.GetKey(KeyCode.Mouse0), is better, it basically is an automatic feature, the problem is that shoots to fast. I used it in conjunction with the above code to achieve a believable rate of fire.

Here is a simple but a bit kludgy way to solve the problem. You are going to remove the for() loop from the ‘shooting’ function (unless you want 10 bullets for every shooting event. Leave the ‘yield’.

Next at the top of the file put:

private var isShooting = false;

Then use this flag to time the shots:

function shooting ()
{   
	if (isShooting) return;
	isShooting = true;
	var hit : RaycastHit;
	if(Physics.Raycast(transform.position, transform.forward, hit,100.0))
	{
		var hitRotation = Quaternion.FromToRotation(Vector2.up, hit.normal);
		if(hit.transform.tag == "Wall")
		{
			Instantiate(bulletHole, hit.point, hitRotation);
	    }
	    hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
	    Debug.Log( " Ray Hit " + hit.collider.gameObject.name);
	}
    yield WaitForSeconds(.08);
    isShooting = false;
}

Note an alternate solution is to put a timestamp or a timer on the code that calls ‘shoot’. Here is a bit of example code. Put it on an empty game object, run the app and hold down the left mouse button:

var delay = 0.1;
var timestamp = 0.0;

function Update() {
	if (timestamp <= Time.time && Input.GetMouseButton(0)) {
		Debug.Log("Shooting");
		timestamp = Time.time + delay;
	}
}