Making Fire Rate for an Input.GetButton

Hi Guys.

I’ve tried every thing i can but any method i use i get the same result.


public int damage = 10;
public float firerate = 0.5f;
public float nextfire = 0.0f;

void Shoot(){

	if (Input.GetMouseButton (0) && Time.time > nextfire ) {

		nextfire =  Time.time + firerate;
	
		RaycastHit hit;
		Ray ray = Camera.main.ScreenPointToRay (new Vector3 (Screen.width * 0.5f, Screen.height * 0.5f, 0f));
		
		if (Physics.Raycast (ray, out hit, 10000)) {
			hit.transform.SendMessage ("applydamage", damage, SendMessageOptions.DontRequireReceiver);

			Debug.Log("BANG!");
		}
	}
		

	}

i’m calling the shoot function every frame but all i get in the log it gets called 4 times roughly every second. why is it being called 4 times each log? also the sendmessage (“applydamge”) isn’t being called properly.

am i doing doing something wrong? is there any other method i could try?

thanks

Try Input.GetMouseButtonDown(0).

Also you should check for Time.time >= nextfire, not Time.time > nextfire.