Unity 5 - Shoot up, Enemy ship does not shoot

Asked

Viewed 194 times

3

The enemy ship simply can’t shoot and walk at the same time, or it does one or the other.

    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 within it I created two more hitR and hitL That’s where the shots come from, the script EnemyGunScript is in the gun and the gun in the enemy ship. Please help me, if you know a possible solution in tmb javascript serves.

  • I don’t understand, the code ta in c# but you want the solution in javascript???

  • If someone knows any possible solution, regardless if it is in C# or Javascript

1 answer

3


Buddy, the way you built your script got kind of complicated to actually work, the way you presented 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 has no sense to own a ship! The ideal is to make a mixture of the 2 classes, logico, fusing the class EnemyGunScript in class EnemyScript causing EnemyGunScript become a method within the class EnemyScript!

This way it becomes more organized and always facilitating maintenance, another detail of this approach is that you will have everything in one place: enemy HP, ammunition, special powers etc...

  • Additional without editing the answer, I say to merge because you are calling Enemygunscript in two objects, making two calls in the Update event and a call in the update for the ship to walk, IE, you have 3 Updates basically in the same Object, so you walk or shoot!

  • It worked, now I just have to adjust some things, thank you

  • ;Quiet D, that’s how it works!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.