I've been tinkering with this for a while and can't seem to get it to work properly, it sometimes works but other times does not.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gun: MonoBehaviour
{
public Transform firePoint;
public GameObject BulletPrefab;
float Speed = 20f;
void Update()
{
if(Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
int a = 0;
float[] b1 = {-15f,0f,15f};
while (a != 3)
{
GameObject bullet = Instantiate(BulletPrefab, firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
Transform tr = bullet.GetComponent<Transform>();
tr.rotation = firePoint.rotation;
tr.Rotate(new Vector3(0f,0f,b1[a]),Space.Self);
rb.AddForce(tr.up * Speed, ForceMode2D.Impulse);
a++;
}
}
}
It's supposed to offset the projectiles at 15 degrees angles and shoot them, but sometimes it just rotates inwards and shoots straight forward.
Tried to convert it to vectors and back but have no clue how to do that and can't seem to find help on this even on page 2 of Google.