public void GunShoot(Turret.WeaponValues weaponValues, bool shoot) { if (shoot && weaponValues.loaded) { weaponValues.loaded = false; GameObject instancedBullet = Instantiate(weaponValues.bulletModel, weaponValues.Barrel.position, weaponValues.Barrel.rotation) as GameObject; instancedBullet.GetComponent <Bullet>().BulletVelocity = weaponValues.projectileSpeed * speedMultiplier; instancedBullet.GetComponent <Bullet>().Damage = weaponValues.baseDamage; instancedBullet.GetComponent <Bullet>().HitMarkerDetector = weaponValues.hitMarker; instancedBullet.transform.rotation = Quaternion.LookRotation(CalculateSpreadVector(weaponValues)); //apply the spread } }
Vector3 CalculateSpreadVector(Turret.WeaponValues weaponValues) //calculates spread and turns it into a vector { var spreadVector = Vector3.zero; var angle = Random.Range(0, 2 * Mathf.PI); //gets a random angle between 0 and 2pi radians. determines which direction the bullet will diviate var offset = Random.Range(0, weaponValues.spread * Mathf.Deg2Rad); //get an angle of how far the bullet will deviate from the centre in radians spreadVector += weaponValues.Barrel.right * Mathf.Cos(angle) * Mathf.Sin(offset); //calculate the x value spreadVector += weaponValues.Barrel.up * Mathf.Sin(angle) * Mathf.Sin(offset); //calculate the y value spreadVector += weaponValues.Barrel.forward * Mathf.Cos(offset); //tbh I think this is a useless value now return(spreadVector.normalized); //normalize it }