简体   繁体   English

Unity:数组中的几个gameObjects(?)

[英]Unity: Several gameObjects in an array(?)

i'm making this beat 'em up game on unity that has the following mechanism: when the player's collider hits an enemy's: 我正在统一制作这种打败游戏,它具有以下机制:当玩家的对撞机击中敌人时:

public GameObject obj;
...

void OnTriggerEnter(Collider collider){
obj=collider.gameObject;
}

Then, when the player attacks, if its distance to the obj is small enough, the enemy receives damage: 然后,当玩家攻击时,如果它与obj的距离足够小,则敌人会受到伤害:

void hit(){
    if(kicked&&close){  //if he is kicking and is close enough
        obj.GetComponent<health>().DealDamage(damage); //this is inside a try/catch, just in case obj=null
(...)
}

The problem is, if 2 enemies touch the player, only the last one will be assigned to the variable obj, so he will take the hit alone, which i don't want to happen. 问题是,如果有2个敌人触摸玩家,则只会将最后一个敌人分配给变量obj,因此他将独自承担打击,我不想发生这种情况。

Any suggestions? 有什么建议么? Should i replace 我应该更换吗

GameObject obj;

with an array? 与数组? How is that? 那个怎么样?

and then, on the OnTriggerEnter method, add objects to the array? 然后,在OnTriggerEnter方法上,将对象添加到数组吗? (how to do that?) (怎么做?)

One simple way is to have your player holding a trigger box. 一种简单的方法是让玩家拿着触发盒。 When the enemies enter the trigger box, they get added to a collection and removed when exiting: 当敌人进入触发框时,它们会添加到集合中,并在退出时被删除:

public class PlayerAttack:MonoBehaviour
{
    private HashSet<Enemy> list = null;
    private void Awake(){ this.list = new HashSet<Enemy>(); }
    private void OnTriggerEnter(Collider col)
    {
         Enemy enemy = col.transform.GetComponent<Enemy>();
         if(enemy != null)
         {
             this.list.Add(enemy);  
         } 
    }
    private void OnTriggerExit(Collider col)
    {
         Enemy enemy = col.transform.GetComponent<Enemy>();
         if(enemy != null && this.list.Contains(enemy) == true)
         { 
             this.list.Remove(enemy);  
         } 
    }
    public void OnAttack()
    {
         foreach(Enemy enemy in this.list)
         { 
            enemy.GetComponent<Health>.DealDamage(damage); 
         }
    }
}

OnAttack is your attack method that will iterate through all close enemies. OnAttack是您的攻击方式,它将遍历所有近距离敌人。 HashSet is a good candidate here as it won't add twice the same item so no risk of seeing an enemy getting multiple hits. HashSet是一个很好的候选对象,因为它不会添加两次相同的项目,因此没有看到敌人受到多次打击的风险。

I didn't manage to solve the problem. 我没有设法解决问题。 The damage never gets to be assigned. 损害永远不会被分配。

private HashSet<Enemy> list = null;

private void Awake(){this.list = new HashSet<Enemy>();}

void OnTriggerEnter(Collider col){

     Enemy enemy = col.transform.GetComponent<Enemy>();
     print(enemy); // this occurs
     if(enemy != null && this.list.Contains(enemy) == true)
     { 
        print(enemy); // this never occurs
         this.list.Remove(enemy);  
     } 
    }
    }


void hit(){      
    print("hit"); // this occurs

    foreach(Enemy enemy in this.list)
     {   // this never occurs
        enemy.GetComponent<health>().DealDamage(damage); 
     }

@Everts @Everts

You have the lists of all objects on screen as enemies, trees, boxes etc. 您具有屏幕上所有对象的列表,如敌人,树木,盒子等。
And in endless cycle you do some actions with all objects: 并且在无尽的循环中,您对所有对象执行一些操作:

while(true)
{
    // Here non combat characters do something.
    foreach(NonCombatCharacter character in NonCombatCharacters)
    {
        switch(character.Type)
        {
            case tinyBird: character.DoAction(Actions.Fly); break;
            case ...
            default:break;
        }
    }
    // Here your combat character aka enemies
    foreach(CombatCharacter character in CombatCharacters)
    {
        // Here you do your combat logic, cheks and etc.
    }
    // And then you player character change the world model, here you
    // process all player action such as changing position, losing hp.
    // And then world keep lifecycle again
}

EDIT: So there is global arrays foreach object. 编辑:所以有针对每个对象的全局数组。 In unity you got them too, even if you dont create the array. 即使您不创建数组,也可以统一获得它们。
Here you go: 干得好:
https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html
Use documentation, most of common answers are all there. 使用文档,大多数常见答案都在那里。

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

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