简体   繁体   English

如何循环我的敌人射击多个目标

[英]How do I loop my enemy shooting multiple targets

I would like to create a Game which is like Slither.io and would want the enemy to shoot the nearest enemy and only shoots once and then stops.我想创建一个类似于 Slither.io 的游戏,并且希望敌人射击最近的敌人并且只射击一次然后停止。 With some help I researched about looping but I'm completely Lost and would like some assistance Thank you在一些帮助下,我研究了循环,但我完全迷路了,需要一些帮助谢谢

public class RandomAIProjectile
{
    private GameObject[] target;
    
    public float speed;
    
    Rigidbody2D bulletRB;
    
    public GameObject explosionEffect;
    
    // Find Targets Section.
    void Start () 
    {
        if (target == null)
            target = GameObject.FindGameObjectsWithTag("Player");
        
        for (int i = 0; i < target.Length; i++) 
        {
            bulletRB = GetComponent<Rigidbody2D> ();
            
            Vector2 moveDir = (target[i].transform.position - transform.position).normalized * speed;
            
            bulletRB.velocity = new Vector2 (moveDir.x, moveDir.y);
            
            Destroy (this.gameObject, 2);
        }
        
    }
    
    private void OnTriggerEnter2D(Collider2D other)
    {
        // Decoy Script To Destroy Players.
        if (other.gameObject.GetComponent<BlackthronpodDiePlayer>() != null)
        {
            Destroy (other.gameObject);
            Destroy (gameObject); 
        }
        // Damage Effect.
        if (other.gameObject.tag == "Player") {
            Instantiate (explosionEffect, transform.position, Quaternion.identity);
            Destroy(gameObject);
        }
        // Player (Bullet) Destroy Enemy.
        if (other.CompareTag ("Bullet"))
        {
            Destroy (other.gameObject);
            Destroy (gameObject); 
        }
        
        
    }
}

Having this in the start method will only run it once the object is initialaized you would want to include a void Update() method to make the object fire every frame.在启动方法中使用它只会在 object 初始化后运行它,您需要包含一个 void Update() 方法来使 object 每帧触发。 Then you could add something like a bool to say that it has fired.然后你可以添加类似 bool 的东西来表示它已经触发。

if(fired)
{ //do nothing}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM