【问题标题】:How do I loop my enemy shooting multiple targets如何循环我的敌人射击多个目标
【发布时间】:2022-01-31 01:48:38
【问题描述】:

我想创建一个类似于 Slither.io 的游戏,并希望敌人射击最近的敌人并且只射击一次然后停止。在一些帮助下,我研究了循环,但我完全迷路了,需要一些帮助谢谢

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); 
        }
        
        
    }
}

【问题讨论】:

标签: c# visual-studio unity3d


【解决方案1】:

在 start 方法中使用它只会在对象初始化后运行它,您可能希望包含一个 void Update() 方法以使对象每帧触发。然后你可以添加类似 bool 的东西来表示它已经触发了。

if(fired)
{ //do nothing}    

【讨论】:

    猜你喜欢
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-27
    • 1970-01-01
    相关资源
    最近更新 更多