How can I decrease accuracy when the gun is fired?

Hi everyone!

I would like someone to tell me how to make the accuracy of the gun decrease when it is fired. The part that fires the raycast is “var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width0.5, Screen.height0.5, 0));” and “if (Physics.Raycast (ray, hit, 500))”. The rest just instantiates a bullet effect. I know that Haim96 answered a similar script, but I didn’t make it clear enough to him.
Thanks, Stormy102

#pragma strict

var Effect : Transform;
var TheDammage = 100;
var gunshotSound : AudioClip;
var reloadGrenade : AudioClip;
var ammoCount : int = 10;
var totalAmmo : int = 120;
var reloadTime : int = 3;
var reloadAmount : int = 10;
var ammo : GUIText;
var gunName : GUIText;
var fireRate : float = 3.0;
var bulletHole : Material;
var force = 10;
private var nextFire : float =0.0;
 
function Update ()
{ 
	Fire ();
	Reload();
	OnGUI();
}
 
function Fire () {
 
	if (ammoCount > 0) {
 
		if (Input.GetButtonDown("Fire1") && Time.time > nextFire) {
 
			nextFire = Time.time + fireRate;
 
			InstantiateGrenade();
 
			ammoCount -=1;
 
		}
 
	}
 
}
 
//---------------------------------------------------------------------------------
 
function InstantiateGrenade () {
	var hit : RaycastHit;
	var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
	
	if (Input.GetMouseButtonDown(0))
	{
		AudioSource.PlayClipAtPoint(gunshotSound, transform.position, 1);
		if (Physics.Raycast (ray, hit, 500))
		{
			var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
			var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
			Instantiate(bulletHole, hit.point, hitRotation);
			Destroy(particleClone.gameObject, 2);
			hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
		}
	}
}
 
//---------------------------------------------------------------------------------
 
function Reload () {
 
	if(ammoCount < 1 && totalAmmo > 10) { //if we have less than 1 bullet then we can reload
 
	if(Input.GetKeyDown("r")) {
 
		AudioSource.PlayClipAtPoint(reloadGrenade, transform.position, 1);
 
		yield WaitForSeconds(reloadTime);
 
ammoCount += reloadAmount;
 
totalAmmo -= reloadAmount;
 
}
 
}
 
}
 
//---------------------------------------------------------------------------------
 
function OnGUI () {
 
ammo.text = "Ammo: " + ammoCount + "/" +  totalAmmo.ToString();
 gunName.text = "Lee-Enfield Mark III";

}

No need to call onGUI() from inside the update function , it already loops on its own.

Make a float, call it RandomAccuracy.

The longer you hold down the mouse button (shoot), it will ADD to that value.

When you’re raycasting out your hit, add the desired location, to a Random(0, RandomAccuracy) to each of your Vertical/Horizontal axis.

Add a timer or some other function, that decreases RandomAccuracy while you’re not holding down the shoot key.

That way, when you shoot once… it should be dead on. If you hold it down, it’d start to go haywire.