敌人预制件不射击

时间:2019-05-07 15:53:21

标签: c# unity3d

我正在创建一个激光防御者游戏,我需要敌人在移动时射击,但是他们没有射击。我创建了一个脚本EnemyShooting,基本上在其中写道,敌人应该射击,但事实并非如此。有人可以查看我的代码并告诉我为什么敌人不射击吗?

下图是敌人的预制检查员: Enemy Inspector}

脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyShooting : MonoBehaviour {

[SerializeField] float EnemyLaserSpeed = 10f;
[SerializeField] float EnemyLaserFireTime;
[SerializeField] GameObject LaserBulletEnemyPreFab;
[SerializeField] int MaxNumberOfHits = 1;
int CurrentNumberOfHits = 0;
Coroutine FireCoroutine;

void OnTriggerEnter2D(Collider2D collider)
{
    if(collider.gameObject.tag == "PlayerLaser")
    {
        if(CurrentNumberOfHits < MaxNumberOfHits)
        {
            CurrentNumberOfHits++;
            Destroy(collider.gameObject);
        }
    }
}

void DestroyEnemy()
{
    if(CurrentNumberOfHits >= MaxNumberOfHits)
    {
        Destroy(gameObject);
    }
}

private void Fire()
{
    FireCoroutine = StartCoroutine(ShootContinuously());
}

void BecomeVisible()
{
    Fire();
}

IEnumerator ShootContinuously()
{
    while (true)
    {
        GameObject LaserBulletEnemy = Instantiate(LaserBulletEnemyPreFab, this.transform.position, Quaternion.identity) as GameObject;
        LaserBulletEnemy.GetComponent<Rigidbody2D>().velocity = new Vector2(0, EnemyLaserSpeed);
        EnemyLaserFireTime = Random.Range(0.5f, 0.9f);
        yield return new WaitForSeconds(EnemyLaserFireTime);
    }
}

// Update is called once per frame
void Update ()
{
    DestroyEnemy();
}

1 个答案:

答案 0 :(得分:1)

您随时都不会调用您的拍摄方法。

按照游戏逻辑,您需要致电BecomeVisible();

// Use this for initialization
void Start () {
  BecomeVisible();
}