Hey guys,

In my MachineGun Script for an Ego-Shooter i’m currently working on uses a Random number to calculate the decreased accuracy when shooting from the hip. I try to achieve this with the use of Random.Range/Random.value like this:

if(!scoping)
{
	var lessaccuracyx = (Random.Range(-0.5, 0.5))*(spread.spread/220);
	var lessaccuracyy = (Random.Range(-0.5, 0.5))*(spread.spread/220);
}
...
	if (Physics.Raycast (cam.position+cam.transform.forward.normalized*1, cam.forward+ Vector3(lessaccuracyx, lessaccuracyy, 0), hit, range, layerMask)) {
...

spread increases when you rapid fire and for that causes the gun to be less accurate over time.

When I fire the gun noscoped at sometimes it seams the work as the bullet holes are scattered like they are supposed to be but after a while the seem to align in a line either completly horizontal, completly vertical or from the top left to the bottom right.

From this behaviour i would assume that the Ramdom value produce similar values.

I’m a bit lost here because sometimes it seems to work fine but then the lines appear.

Can someone please help me? If you need more information please let me know.

Well i looked over my script again and came to the conclusion that i made it overly complex so I changed it to this:

	var spreadfloat : float = ((spread.spread/100)/2);
	lessaccuracyx = Random.Range(-spreadfloat, spreadfloat);
	lessaccuracyy = Random.Range(-spreadfloat, spreadfloat);

spreadfloat is somewhere around 0.25…
Still the bulletHole seem to align roughly around a line from the top left to the bottom right.

Example:

8893-picaccuracy.jpg

The raycast is using x & y along a global axis. It should be a local axis. When you face due North or South, it should work mostly fine (since your right is also +x,) but as you turn East/West, it will get strange (since +x is now away/towards you.) To get local:

startPos = cam.position + cam.forward; // same as before
rayDir = cam.forward + cam.right*lessAccurX + cam.up*lessAccurY; // <-- local axis
Phy.Raycast( startPos, rayDir, ....);

NOTES: your code normalizes cam.forward. No need, since it already is. If you need to push it forwards more, cam.forward*5 works.

For aiming, can also use cam.transformDirection(lessAccurX, lessAcurY, 0); as a replacement for cam.right and cam.up. Both ways say to take lessAcurX/Y and twist it to the way you are facing.