Enemy projectile direction setting

Hey everyone. I’ve run into an issue with using “Turret-type” enemies in a simple 2D game, programmed to fire a projectile based on the direction and rotation it’s currently in. The problem is that when I have two facing different directions, their projectiles get ‘confused’ as to which way to go, is the best way to put it. I have tested it with just one enemy and it works fine. Is there a better way to make sure that the bullet ‘knows’ specifically which direction it was fired from? Thanks in advance. For reference, here’s the code for the bullet in question I’m using:

    public class EBullet1Behavior : MonoBehaviour {
    
    	private Rigidbody2D bulletRigi;
    	public Playermovement playerMovement;
    	public EnemyType2Properties enemyType2;
        public FindVector findVector;
    
        public Transform bulletPoint;
    
        public float speed;
    	public float destroyTime;
    
    	// Use this for initialization
    	void Start () {
    
            playerMovement = FindObjectOfType<Playermovement> ();
    	enemyType2 = FindObjectOfType<EnemyType2Properties> ();
            findVector = FindObjectOfType<FindVector>();
    	bulletRigi = GetComponentInParent<Rigidbody2D> ();
    
    
                bulletRigi.velocity = new Vector2 (findVector.x * speed, findVector.y * speed);
    		transform.right = new Vector3(findVector.x, findVector.y, 0);
    
    
    
        }
    	
    	// Update is called once per frame
    	void Update () {
    		Destroy (gameObject, destroyTime);
    	}
    }

While the firing bit is fairly simple:

    IEnumerator Firing() {
        if (stunned == false && canFire == true)
        {
            Instantiate(EnemyBullet, bulletPoint.position, bulletPoint.rotation);
            yield return new WaitForSeconds(fireSpeed);
            Debug.Log("Fire!");
            canFire = true;
 // Firing capability is turned 'off' elsewhere in the script
            Update();
        }
    }

The bullet has a ton of properties it doesn’t need to have. It’s also getting it’s direction by finding a FindVector object, and grabbing information from that.

The FindObjectOfType method finds an object of that type. Which object it finds is not guaranteed, but it’s usually the same one for every call on the same frame, so both your guns’ bullets are finding the same FindVector object.

The solution here is to not use the convoluted FindVector method, and skip the start method. Instead, give the bullet a Fire method that takes the direction. So along the lines of:

//bulletscript
public class BulletScript : MonoBehaviour {

    public float speed; //set in prefab
    public float destroyAfter; // set in prefab

    public void Fire() {
        GetComponent<RigidBody>().velocity = transform.forward * force;
        Destroy(gameObject, destroyAfter);
    } 
}

That’s really all you need. Your firing code will simply be:

GameObject bulletObj = Instantiate(EnemyBullet, bulletPoint.position, bulletPoint.rotation);
bulletObj.Fire();

The Instantiate call sets the rotation of the bullet. This means that if the Fire-code sets the velocity straight forward (transform.forward), it should go straight ahead.