简体   繁体   English

unity 3D 统计代理通过门数

[英]Unity 3D Counting Number of Agent Passes through a door

I am trying to make fire evacuation simulation by using Unity.我正在尝试使用 Unity 进行火灾疏散模拟。 I need to count the number of agent passes through the exit door during the evacuation.我需要计算疏散期间特工通过出口门的次数。 Is there any way to do it?有什么办法吗?

You could set up a simple trigger collider system.您可以设置一个简单的触发对撞机系统。

First, you would place a box collider at the exit door, and set it to trigger so it isn't a solid object (objects can path through it, rather than walk into it).首先,您将在出口门处放置一个盒子对撞机,并将其设置为触发,这样它就不是固体 object(物体可以穿过它,而不是走进它)。 Now, add a Rigidbody to this box collider, and set first drop-down menu that says 'Dynamic' to 'Kinematic'.现在,为这个盒子碰撞器添加一个刚体,并将第一个显示“动态”的下拉菜单设置为“运动学”。 Now, a way to count them.现在,一种计算它们的方法。 We will ad the following script to the box collider object:我们将以下脚本添加到盒子碰撞器 object:

using UnityEngine;

public class ExitDoor : MonoBehavour
{
   void OnTriggerEnter(Collider obj)
   {
       if (obj.gameObject.tag == “agent”)
       {
       }
   }
}

This doesn't work yet, because we don't have anything other than an OnTriggerEnter statement.这还行不通,因为除了OnTriggerEnter语句之外我们没有其他任何东西。 OnTriggerEnter is called every time either a game object passes through a trigger collider or this game object passes through a trigger collider.每次游戏 object 通过触发对撞机或此游戏 object 通过触发对撞机时,都会调用 OnTriggerEnter。 We set up an if statement to detect if the game object that passed through it has a certain tag.我们设置了一个if语句来检测通过它的游戏object是否有某个标签。 We are searching for a tag called “agent”.我们正在搜索一个名为“代理”的标签。 Set each agent's tag to “agent”.将每个代理的标签设置为“代理”。 Now we should start a counting system.现在我们应该启动一个计数系统。

using UnityEngine;

public class ExitDoor : MonoBehavour
{
   public int agents;

   void OnTriggerEnter(Collider obj)
   {
       if (obj.gameObject.tag == “agent”)
       {
           agents += 1;
       }
   }
}

Now, we add 1 to a variable each time an agent enters the collider.现在,每次代理进入对撞机时,我们将变量加 1。 The only problem with this is that if the agent goes through the collider twice, it will count it twice.唯一的问题是,如果代理通过对撞机两次,它会计算两次。

This system is done, but now you might want to access this from different scripts.该系统已完成,但现在您可能希望从不同的脚本访问它。 We will use GetComponent<>() to access the script.我们将使用GetComponent<>()来访问脚本。 Add this to your game management script, or whatever you want to be accessing this:将此添加到您的游戏管理脚本,或者您想要访问的任何内容中:

using UnityEngine;

public class GameManagement : MonoBehavour
{
   public GameObject ExitDoor;
   public int agents;

   void Update()
   {
      agents = ExitDoor.GetComponent<ExitDoor>().agents;
      if (agents == 10)
      {
         Debug.Log(“10 agents have exited.”);
      }
   }
}

Now, we access the script ExitDoor , and get agents from it.现在,我们访问脚本ExitDoor ,并从中获取代理 Make sure you set the ExitDoor variable from the game management script to the box collider game object used for counting.确保将游戏管理脚本中的ExitDoor变量设置为用于计数的盒子碰撞游戏 object。

This was untested and if you get any bugs or this wasn't what you wanted, comment on this post to ask me.这是未经测试的,如果您遇到任何错误或这不是您想要的,请在此帖子上发表评论以询问我。

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

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