简体   繁体   English

Unity 3D:游戏对象交互

[英]Unity 3D: Game Object interaction

Assuming we have one first person character and several NPC AIs. 假设我们有一个第一人称角色和几个NPC AI。 The first person character should be able to talk to the NPC AI's (via dialog service). 第一人称角色应该能够与NPC AI交谈(通过对话服务)。 My problem is: How should I "connect" the two game objects (FPC and NPC) to each other? 我的问题是:我应该如何将两个游戏对象(FPC和NPC)相互“连接”?

So he can basically walk around and talk to them, then they answer something, and the first person character may choose an option. 所以他基本上可以四处走动并与他们交谈,然后他们回答一些问题,第一个人可能会选择一个选项。

I see several options here: 我在这里看到几个选项:

  1. add a public variable to 'first person character'. 将公共变量添加到“第一人称角色”。 this public variable will hold the NPC AI. 这个公共变量将持有NPC AI。 So the NPC AI and first person character can talk to each other via functions on both classes. 所以NPC AI和第一人称角色可以通过两个类的功能相互交谈。
  2. Add a class 'DialogService' which implements the observer pattern. 添加一个实现观察者模式的类'DialogService'。 So there will be several publishers and subscribers. 因此会有几个发布者和订阅者。 My problem here is how to hook up the NPCs and first person character to each other 我的问题是如何将NPC和第一人称角色相互联系起来
  3. Maybe you have a better alternative? 也许你有更好的选择?

If you're looking for a simple solution, I could offer this: 如果您正在寻找一个简单的解决方案,我可以提供:

Inside of the character's GameObject have another GameObject called, let's say, "Dialog Collider". 在角色的GameObject里面有另一个GameObject ,比方说,叫做“Dialog Collider”。 Add a collider component to that game object, make it (the collider) a trigger. 将碰撞器组件添加到该游戏对象,使其(碰撞器)成为触发器。 Also, I would advise you to make the collider a cylinder with a larger radius so that you can enter dialog without actually colliding with another character. 另外,我建议你让对撞机成为一个半径更大的圆柱体,这样你就可以进入对话框,而不会与另一个角色发生碰撞。

Add a script your player character that starts the dialog when the "Dialog Collider" enters the collision with another GameObject of same name/tag. 当“Dialog Collider”与另一个同名/标记的GameObject发生冲突时,添加一个脚本,即启动对话框的玩家角色。 At this point I presume you would have the same setup for all game character: they all should have a "Dialog Collider" GameObject inside of the main GameObject, if they have a dialog option to begin with that is. 在这一点上我想你将不得不对所有游戏角色相同的设置:他们都应该有一个“对话撞机” GameObject主要游戏对象的内部,如果他们有一个对话框,选择开始与是。

That way you can leave the collision detection to the UnityEngine and not check for it every frame. 这样你就可以将碰撞检测留给UnityEngine而不是每帧检查一次。

Also, I'd advise to make a generic Dialog script; 另外,我建议制作一个通用的Dialog脚本; make it an abstract class that you can inherit from to override some methods so you'd have all the basic functionality right from the start and enough flexibility to change/extend the behaviour of the NPC later on. 使它成为一个抽象类,你可以继承它来覆盖一些方法,这样你就可以从一开始就拥有所有的基本功能,并有足够的灵活性来改变/扩展NPC的行为。

Or make a IDialogCharacter interface and implement the methods and dialogs manually for every class; 或者制作一个IDialogCharacter接口,并为每个类手动实现方法和对话框; use polymorphism to your advantage to check for the IDialogCharacter interface if you prefer composition over inheritance. 如果您更喜欢组合而不是继承,请使用多态来检查IDialogCharacter接口。

As the hooking up part - when the collision begins your player character will be able to fetch the script (first get the colliding GameObject, then the script component) of the other character and request the dialog start from the other character or get some data object from the other character and start the dialog. 作为连接部分 - 当碰撞开始时,你的玩家角色将能够获取另一个角色的脚本(首先获得碰撞的GameObject,然后是脚本组件)并请求对话从另一个角色开始或获取一些数据对象从另一个角色开始对话。

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

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