- Home /
two Random.Range returning similar values
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:
This probably isn't it, but it is a good idea to put an 'f' at the end of your float values to enforce the type: Random.Range(-0.5f, 0.5f)
What kind of value is spread.spread/220 returning? If it is a value less than one, then yes you are going to generate some very low numbers. If you are unsure of the values returned by Random, isolate them and exa$$anonymous$$e them in the inspector or in a console debug :
if(!scoping)
{
var rndX : float = Random.Range(-0.5, 0.5);
var rndY : float = Random.Range(-0.5, 0.5);
Debug.Log( "rndX = " + rndX + " : rndY = " + rndY );
var lessaccuracyx : float = rndX *(spread.spread/220);
var lessaccuracy : float = rndY *(spread.spread/220);
}
Thank you for your reply. Yes the values I'm working with are lower than 1. They range around -0.25 to +0.25 .
I did a test just guessing at the spread value and get some consistently spread values. No lines for me. $$anonymous$$aybe the problem is in your raycasting? I don't do much raycasting so I am unfamiliar with the syntax.
Answer by Owen-Reynolds · Mar 13, 2013 at 04:26 PM
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.