- Home /
 
 
               Question by 
               KUFgoddess · Jan 12, 2018 at 10:28 PM · 
                c#2dprojectile  
              
 
              How to shoot 2d projectile in multiple directions 1 by 1? Left Right Up Down
I want to give this enemy the ability to shoot bullets left right up and down I am still new to prjectiles so excuse me if I am not writing the proper code 
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class RotoShooter : MonoBehaviour {
     [SerializeField]
     private GameObject bullet;
     private GameObject bullet2;
     private GameObject bullet3;
     private GameObject bullet4;
     // Use this for initialization
     void Start () {
         StartCoroutine(Attack());
     }
 
 
 
 
     IEnumerator Attack ()
     {
 
         yield return new WaitForSeconds(Random.Range(2, 7));
 
         Instantiate(bullet, transform.position, Quaternion.identity);
 
         yield return new WaitForSeconds(Random.Range(2, 7));
         Instantiate(bullet2, transform.position, Quaternion.identity);
         yield return new WaitForSeconds(Random.Range(2, 7));
         Instantiate(bullet3, transform.position, Quaternion.identity);
         yield return new WaitForSeconds(Random.Range(2, 7));
         Instantiate(bullet4, transform.position, Quaternion.identity);
 
     }
     // Update is called once per frame
     void OnTriggerEnter2D (Collider2D target) {
         if (target.tag == "Player")
         {
             Destroy(target.gameObject);
 
         }
     }
 }
 
               
                 
                pyxeledit-2018-01-12-15-49-32.png 
                (13.4 kB) 
               
 
              
               Comment
              
 
               
              Answer by ElegantUniverse · Dec 14, 2018 at 09:00 AM
hello
check this video out. https://www.youtube.com/watch?v=kOzhE3_P2Mk
Answer by cdr9042 · Dec 14, 2018 at 09:18 AM
Instead of assigning 4 different Game Object for each direction, you only need one. Add a BulletScript to the bullet prefab, a Rigidbody2D, the script should have this function:
 [SerializeField] Rigidbody2D m_RigidBody; //assign the bullet's rigidbody here
 public void Init(Vector3 direction)
     {
         m_RigidBody.AddForce(direction);
     }
 
               To shoot a bullet, call this from the shooter:
 GameObject tempBullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity, null);
 tempBullet.GetComponent<BulletScript>().Init(Vector3.up); //Vector3.up can be the direction you want here
 
              Your answer