How do you make bullet Spread?
I am trying to make a gun script but I am having trouble with the bullet spread. I have tried to randomise the direction but it stays shooting in a straight line. Please leave an answer if you can help. Thanks
public float damage = 10f;
public float range = 100f;
public float firerate = 15f;
public float normalSpread = 0.04f;
public Camera fpsCam;
public ParticleSystem muzzleFlash;
public AudioSource GunSound;
private float NextTimeToFire = 0f;
// Update is called once per frame
void Update () {
if (Input.GetButton ("Fire1") && Time.time >= NextTimeToFire)
{
NextTimeToFire = Time.time + 1f / firerate;
Shoot ();
}
}
void Shoot ()
{
muzzleFlash.Play ();
GunSound.Play ();
RaycastHit hit;
Vector3 direction = fpsCam.transform.forward; // Bullet Spread
if (Physics.Raycast (fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
direction.x += Random.Range(-normalSpread, normalSpread);
direction.y += Random.Range(-normalSpread, normalSpread);
direction.z += Random.Range(-normalSpread, normalSpread);
Debug.Log (hit.transform.name);
Enemy enemy = hit.transform.GetComponent<Enemy> ();
if (enemy != null)
{
enemy.TakeDamage(damage);
}
}
}
},
Comment