Randomized Gun Spread

I want a gun that changes the position a little as if it was inaccurate. heres my script. also it doesn’t seem to be working

var Damage = 1;
var spread : float = 0.25;
var Cam : Camera;

private var lineTransform : Vector3;
private var startTransform : Vector3;


function Update ()
{
	lineTransform = transform.position;
	startTransform = transform.position;

	var hit : RaycastHit;
	var ray : Ray = Cam.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5,0));
	
	Debug.DrawRay(startTransform, lineTransform, Color.red);
	
	if (Input.GetMouseButtonDown(0))
	{
		if (Physics.Raycast (ray, hit, 500))
		{
			hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);					
			lineTransform = hit.point;
		} 
	}
}

21505-awerds.png

The variable spread doesn’t appear in this script. You are setting lineTransform at start of Update, setting the line transform variable at the end of the Update method, and not using it again after that. Did you mean to set it at start of Update? Also, in the RayCast, you probably want to set it to hit.Point - lineTransform because You are passing lineTransform in as the ray direction value. Go here to see how to properly use drawRay:

For creating a random range between two floats, use:
float ran = Random.Range(-1.0f, 1.0f);

For instance, this produces a random range between -1 and 1.