I am setting up a gameobject that uses a coroutine to endlessly spawn bullets, and I want to make it so that when I rotate the gameobject it also changes the direction the bullets are spawning and moving at. Currently, the projectiles do not move at all.
The first if statement is determined by a public bool laser, which I have toggled in the editor for specific prefabs. I have declared _bullet and _laser as GameObjects and serialized them so I can plug them in the editor. I have also declared a public RigidBody rb but haven’t assigned anything to it in the editor. The Rigidbodies are kinematic.
IEnumerator Shoot()
{
while (true)
{
yield return new WaitForSeconds(1);
if (laser == false)
{
Instantiate(_bullet, transform.position + new Vector3(0, 0, 0), Quaternion.identity);
rb = _bullet.GetComponent<Rigidbody2D>();
rb.AddForce(new Vector2(10, 0));
}
else if (laser == true)
{
Instantiate(_laser, transform.position + new Vector3(0, 0, 0), Quaternion.identity);
rb = _laser.GetComponent<Rigidbody2D>();
rb.AddForce(new Vector2(10, 0));
}
}
}