Bullet Spread Problem

I have created this simple bullet spread when fired, but it seems to only work on a diagonal line. Here’s the code (It’s bad I know, I only just started coding):

#pragma strict

var Effect : Transform;
var bulletHole : GameObject;
var TheDamage = 100;
var fireSpeed : float = 15;
var Spread = 5;
var waitTilNextFire : float = 0;

function Update(){
	var FireSpread = Random.Range(0 - Spread,Spread);
	var hit : RaycastHit;
	var ray : Ray = Camera.main.ScreenPointToRay(Vector3((Screen.width * 0.5 )+- FireSpread, (Screen.height * 0.5 )+- FireSpread));
		if(waitTilNextFire <= 0){
			waitTilNextFire =1;
			if(Physics.Raycast(ray, hit, 100)){
				var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
				Instantiate(bulletHole, hit.point, hitRotation);
				var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
				Destroy(particleClone.gameObject, 2);
				hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);

	waitTilNextFire -= Time.deltaTime * fireSpeed;

Here’s a picture of the issue :

Any help given will be greatly appreciated, thank you.

That is because you are using the same random value (FireSpread) for the x and the y value in the ScreenPointToRay function. This means it will always go in an equal amount in horizontal as in vertical, and therefore create a diagonal spread. Instead you should use two random values, one for x and another one for y.


var FireSpreadX = Random.Range(-Spread,Spread);
var FireSpreadY = Random.Range(-Spread,Spread);
var ray : Ray = Camera.main.ScreenPointToRay(Vector3((Screen.width * 0.5) - FireSpreadX, (Screen.height * 0.5) - FireSpreadY));


Firstly we must find the part where the random values are calculated, and then we can take a look at the distribution pattern and see what’s wrong.

On line 14 you calculate one random value:

//this code is corrected to match standards
var FireSpread:float = Random.Range(-Spread, Spread);

And on line 16 you use it:

//again, fit to match standards and good coding practices
var screenPoint:Vector3 = Vector3(Screen.width/2 - FireSpread,
                                  Screen.height/2 - FireSpread));
var ray:Ray = Camera.main.ScreenPointToRay(screenPoint);

So now we can derive the distribution pattern of the screen point using a set of equations:

Let screenPoint be the vector (x, y). Let Screen.width be the constant w and Screen.height be the constant h. Let FireSpread be λ.

x = w/2 - λ
y = h/2 - λ

or in the vector equation form:

(x, y) = (w/2, h/2) - (λ, λ)
r = 0.5*(w, h) - λ*(1, 1)

This is, as seen, the equation of a line, explaining why your distribution pattern was a diagonal line.

To fix this, you will need to have the distribution of a plane, and not a line. For this you will need another variable. Using the standard vector equation of a line we get:

r = 0.5*(w, h) - λ*(1, 0) -  γ*(0, 1)

Now we can backtrack this back to the code:

(x, y) = (w/2, h/2) - (λ,  γ)
x = w/h - λ
y = h/2 - γ
var screenPoint:Vector3 = Vector3(Screen.width/2 - FireSpread,
                                  Screen.height/2 - γ));

So now you need to calculate γ, which is just another random variable.

I would also suggest using a Vector2 for your random variables instead of individuals.

Hope this helps,