Unity 5 - Shoot up, enemy ship does not shoot

3

The enemy ship simply can not shoot and walk at the same time, or it does one thing or another.

    public class EnemyScript : MonoBehaviour {

    public float speed = 5f;


    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

        Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0,0));

        transform.Translate (new Vector3(0,1,0) * speed* Time.deltaTime);

        if (transform.position.y < min.y) {
            Destroy(gameObject);
        }
    }
}



    ublic class EnemyGunScript : MonoBehaviour {

    public GameObject HitL;
    public GameObject HitR;
    public GameObject EnemyBullet;
    public float nextFire = 1f;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
        Invoke("FireEnemyBullet", 1f);
    }

    void FireEnemyBullet(){

        GameObject playerShip = GameObject.Find ("Player");

        if (playerShip != null && Time.time*3 >= nextFire) {

            GameObject bullet01 = (GameObject)Instantiate (EnemyBullet);
            bullet01.transform.position = HitL.transform.position;

            GameObject bullet02 = (GameObject)Instantiate (EnemyBullet);
            bullet02.transform.position = HitR.transform.position;

            //Vector2 direction = playerShip.transform.position - bullet.transform.position;

            //bullet01.GetComponent<EnemyBullet>().SetDirection(direction);

            //nextFire += Time.time/3;

            nextFire += 1.5f;

        } 

    }


}
I created a GameObject called gun and inside it I created two more hitR and hitL q is where the shots exit, the EnemyGunScript script is in gun and gun in the enemy ship. Please help me if you know a possible solution in javascript tmb serves.

    
asked by anonymous 03.01.2016 / 01:30

1 answer

3

Friend, the way you built your script was kind of complicated to actually work, the way you introduced the code to understand that the class was never called!

Another detail is that you have built an enemy class:

public class EnemyScript : MonoBehaviour {

And also built another class for the enemy to shoot:

public class EnemyGunScript : MonoBehaviour {

The class that makes the enemy shoot does not own a ship! Ideally, you merge the 2 classes, logically, merging the class EnemyGunScript into the EnemyScript class causing EnemyGunScript to become a method within the EnemyScript class!

In this way you get more organized and always easier to maintain, another detail of this approach is that you will have everything in one place: HP of the enemy, ammunition, special powers etc ...

    
03.01.2016 / 03:08