With Unity’s InputSystem在使用C#事件时是否有一种方法来区分玩家?

huangapple go评论106阅读模式
英文:

With Unity's InputSystem is there a way to discriminate between players when using c# events?

问题

我正在尝试使用Unity,并创建了一个多人本地合作环境。我有一个Player Input Manager,在每次新设备连接时创建新玩家。这些玩家具有一个使用C#事件的Player Input组件。然后,我有一些代码监听这些事件并记录到控制台。我希望能区分不同的玩家,但目前每个设备上的每个输入都会触发事件。

我已经观看了无数关于输入系统的教程,但似乎从未涉及本地多人游戏。我能够使用send、broadcast和Unity的事件使其工作,但我想知道如何使用C#事件实现这一目标。

英文:

I am experimenting with Unity and have created a multi-player local coop environment. I have a Player Input Manger create a new player whenever a new device connects. These players have a Player Input component that is set to use c# events. Then I have some code that listen for the events and logs to the console. I want to be able to distinguish between players, but at the moment events get fired for every input on every device.

I have watched countless tutorials on the Input System, but local multiplayer never seemns to be addressed. All the solutions I can think off seem nasty. I can get it working with send, broadcast and unity's events, but I would like to know how to achive it with c# events.

答案1

得分: 0

不通过事件系统可能无法实现,但如果直接使用输入管理器(例如 Input.GetButtonDown()),则可以在项目设置中设置按钮和轴以支持游戏手柄。

基本上,您需要在项目设置的输入管理器中复制所有按钮和轴,以支持您想要的玩家数量,然后逐个设置摇杆索引为固定的自定义值。

然后,例如,不再有“Jump”按钮,而是有“Jump 1”到“Jump 4”,每个都适用于不同的游戏手柄。

有关更多信息,请参阅虚拟轴表中的最后一项:
https://docs.unity3d.com/Manual/class-InputManager.html

英文:

I don't think you can through the event system, no.

However, if you use the Input Manager directly (i.e. Input.GetButtonDown()), then you can go into your project settings and set up the buttons and axes to be gamepad specific.

Essentially, what you need to do is duplicate all of the buttons and axes in the Project Settings Input Manager for however many players you want to support, and then go through them and set the Joystick index to a fixed custom value.

Then, instead of having, for example, a "Jump" button, you'll have "Jump 1" through "Jump 4", each unique to its own gamepad.

See the last entry in the Virtual Axes table for more info:
https://docs.unity3d.com/Manual/class-InputManager.html

huangapple
  • 本文由 发表于 2023年2月13日 23:51:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定