- Home /
Need some help with bullets spread fire!!!
Here i'm developing a top down space shooter game where the player shoots bullets in upward direction , i'm trying to implement some power-up among which i'm stuck with the spread fire , where i want to spread the bullets in a particular angle. This is the code i'm using
[SerializeField] int bulletCount;
[SerializeField] float spreadAngle;
List<Quaternion> bullets;
void Start()
{
bullets = new List<Quaternion>(bulletCount);
for(int i = 0 ; i<bulletCount ; i++)
{
bullets.Add(Quaternion.Euler(Vector3.zero));
}
}
public void Fire()
{
if(GameObject.Find("Player") !=null)
{
for (int i = 0; i < bulletCount; i++)
{
bullets[i] = UnityEngine.Random.rotation;
var FireBullet = FindObjectOfType<Player>();
GameObject Projectile = Instantiate(Laser, new Vector3(FireBullet.transform.position.x, FireBullet.transform.position.y + 0.5f,FireBullet.transform.position.z),transform.rotation) as GameObject;
Projectile.transform.rotation = Quaternion.RotateTowards(Projectile.transform.rotation, bullets[i], spreadAngle);
Projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(0, FireDistance);
Projectile.GetComponent<Rigidbody2D>().AddRelativeForce(Projectile.transform.forward * 10f);
}
}
}
The bullets gets rotated but they all stick together and move upwards, there is no spread happening. please can someone help me out with this kinda stuck on it for a while... Thanks in advance.:)
Answer by tormentoarmagedoom · May 24, 2019 at 11:18 AM
Hello.
You should imstantiate new bullets at the spread position, and give them velocity in an angle. AS i see you are very close. You shoulddebug your code line by line while running to detect where the code is not doing what you expect.
Bye!