I have been trying to make bullets spread out in a non-random way when shooting, similar to TF2’s competitive mode, where all the bullets go in 3 straight horizontal lines, and form a square. (here is an example of it) With my current script, they all go in straight lines when the player is looking at a 90 degree angle, but spread diagonally when looking at a bigger or smaller angle. (the bullets go in a vertical line at 180 degrees)
Here is an example of the problem : Imgur: The magic of the Internet The main holdup seems to be that my script doesn’t properly take account of the player’s y rotation when spreading out the bullets. No matter what I try, it never seems to…
The script :
public GameObject bullethole;
private float NextTimeToFire = 0f;
List<Vector3> s = new List<Vector3>();
public int currentAmmo = 4;
public float firerate = 0.625f;
public float reloadTime = 0.6f;
public float timer;
public float damage = 6f;
public Vector3 direction;
public Vector3 spread;
public float range;
public Camera cam;
void Start()
{
//this is a list of predetermined spread positions
s.Add(new Vector3(0, 0));
s.Add(new Vector3(0, 0.05f));
s.Add(new Vector3(0, -0.05f));
s.Add(new Vector3(0.05f, 0));
s.Add(new Vector3(0.05f, 0.05f));
s.Add(new Vector3(0.05f, -0.05f));
s.Add(new Vector3(-0.05f, 0, 0));
s.Add(new Vector3(-0.05f, 0.05f));
s.Add(new Vector3(-0.05f, -0.05f));
}
void Update()
{
if (currentAmmo == 0)
{
timer += Time.deltaTime;
if (timer >= reloadTime)
{
timer = 0;
currentAmmo = 4;
}
}
if (Input.GetButtonDown("Fire1") && currentAmmo != 0 && Time.time >= NextTimeToFire)
{
NextTimeToFire = Time.time + firerate;
for (int i = 0; i < s.Count; i++)
{
//for each predetermined spread position in the list, shoot
RaycastHit hit;
spread = transform.TransformDirection(s[i]);
Vector3 direction = spread + cam.transform.forward;
//convert the spread to world space and add it to the look direction
if (Physics.Raycast(cam.transform.position, direction, out hit))
{
GameObject hole = Instantiate(bullethole, hit.point + new Vector3(0, 0, 0.3f), Quaternion.LookRotation(-hit.normal));
Destroy(hole, 5f);
Health target = hit.transform.GetComponent<Health>();
if (target != null)
{
target.TakeDamage(damage);
}
}
if (i == 10)
{
currentAmmo -= 1;
}
}
}
}
If someone can find a solution to this it would be a godsend…



