简体   繁体   English

Unity Input controller 关于新手 Controller

[英]Unity Input controller about new Hand Controller

I have build a driver simulator using Unity and I use as steering wheel the Logitech G29 controller. So in my project to break and throttle I configured this:我已经使用 Unity 构建了一个驾驶员模拟器,我将 Logitech G29 controller 用作方向盘。所以在我的项目中打破和节流我配置了这个:

在此处输入图像描述

Vertical1 is used to Throttle function and Vertical2 is used to Break function. This configuration are working now. Vertical1 用于 Throttle function,Vertical2 用于 Break function。此配置现在有效。

Now I need to configure also another controller (HC1 3DRap).现在我还需要配置另一个 controller (HC1 3DRap)。 This is an Hand Controller. So I checked it on windows device and I can see this:这是一只手 Controller。所以我在 windows 设备上检查了它,我可以看到:

在此处输入图像描述

Rotation Axis X and Rotation Axis Y have a value in sleep mode (without press the two levels). Rotation Axis X 和 Rotation Axis Y 在休眠模式下有一个值(不用按两档)。

Now I need to integrate also this new Controller in my project.现在我还需要将这个新的 Controller 集成到我的项目中。 So I try to make this:所以我试着这样做:

在此处输入图像描述

In this mode if I try to check value of Y axis value with the follow code ( in this moment I cannot press the levers):在这种模式下,如果我尝试使用以下代码检查 Y 轴值的值(此时我无法按下控制杆):

Debug.Log("Input debug frenata: " + Input.GetAxis("Vertical2"));

I can display this:我可以显示这个:

在此处输入图像描述

If I try to press a levers, I can display this values如果我尝试按下控制杆,我可以显示这些值

在此处输入图像描述

In this mode with thie new controller join on the system I m not able to run the car, because I think that there is every time the break pressed.在这种模式下,新的 controller 加入系统,我无法运行汽车,因为我认为每次按下中断键。

Could you suggest me, how can I fixed this bug?你能建议我,我该如何修复这个错误?

I run into a similar problem some time ago.我前段时间遇到了类似的问题。 I found out, that the axis I was actually using was not the one I expected.我发现,我实际使用的轴不是我预期的轴。 Let's say you have a joystick and have a separate "POV-Stick" on it.假设您有一个操纵杆,上面有一个单独的“POV-Stick”。 When you use the POV-Stick you might be moving the whole joystick and therefore change the main axis of the joystick.当您使用 POV-Stick 时,您可能会移动整个操纵杆,因此会改变操纵杆的主轴。 If you are just watching the main axis input, it looks like that is the input of your POV-Stick, but actually isn't.如果您只是看主轴输入,它看起来像是您的 POV-Stick 的输入,但实际上不是。 So make sure the input you read is the correct one.因此,请确保您阅读的输入是正确的。

Then you have another problem: Not every joystick, steer etc. is mapping it's inputs to the same axis.然后你会遇到另一个问题:并非每个操纵杆、转向等都将其输入映射到同一轴。 So if you buy 2 more devices, they might be on a different axis as well.因此,如果您再购买 2 台设备,它们也可能位于不同的轴上。 If you try to handle that on your own, you go crazy.如果你试图自己处理,你 go 疯了。

There is a unity forum about that topic (and other related problems).有一个关于该主题(以及其他相关问题)的统一论坛 And I found that there are some unity plugins, that could probably solve your problem:我发现有一些 unity 插件可以解决你的问题:

I hope you can solve your problem with these inputs (please let us know, if you do).我希望你能用这些输入解决你的问题(如果你这样做,请告诉我们)。

It seems that since you're seeing values even when the input device is not being used, that you'll need to "zero" the device and use dead zones like you might for a joystick or controller.似乎因为即使输入设备未被使用时您也会看到值,所以您需要将设备“归零”并使用死区,就像您可能对操纵杆或 controller 一样。

In the Player or Input script, in OnEnable/Awake/Start (whichever works best for you), set the "zero" value for the device in a field.在播放器或输入脚本中,在 OnEnable/Awake/Start(最适合您的那个)中,在字段中为设备设置“零”值。 You can run the assignment of the Vertical2 zero value in a method, and then allow the player to recalibrate during play with a button that invokes that function.:您可以在方法中运行 Vertical2 零值的分配,然后允许播放器在播放过程中使用调用该 function 的按钮重新校准:

private float _vertical2Zero = 0.0f;    

void Start()
{
     this.CalibrateVertical2Zero();

     // ... more code ...
}

private void CalibrateVertical2Zero()
{
    this._vertical2Zero = Input.GetAxis("Vertical2")
{

Then, when you're checking the value later, test it against the "zero" value, and apply some deadzones if desired:然后,当您稍后检查该值时,根据“零”值对其进行测试,并根据需要应用一些死区:

private float _vertical2Deadzone = 0.05f;

void HandleInput()
{
    float newVertical2Value = Input.GetAxis("Vertical2");
    bool vertical2Low = newVertical2Value <= ( this._vertical2Zero - _vertical2Deadzone );
    bool vertical2High = newVertical2Value >= ( this._vertical2Zero + _vertical2Deadzone ); 


    if( vertical2Low || vertical2High )
    {
        // Input detected on Vertical2, accounting for the zero and deadzone
    }
}

This can help with your problem. 可以帮助您解决问题。

How to manage input in Unity如何在 Unity 中管理输入

void Update() 
{ 
    if (Input.GetKeyDown(KeyCode.Space)) 
    { 
        // Spacebar was pressed 
    } 
        if (Input.GetMouseButtonDown(0)) 
    { 
        // Left mouse was pressed 
    } 
}

The new Input System新的输入系统

using UnityEngine;
using UnityEngine.InputSystem;
public class ReportMousePosition : MonoBehaviour
{
    void Update()
    {
        Vector2 mousePosition = Mouse.current.position.ReadValue();
        if(Keyboard.current.anyKey.wasPressedThisFrame)
        {
            Debug.Log("A key was pressed");
        }
        if (Gamepad.current.aButton.wasPressedThisFrame)
        {
            Debug.Log("A button was pressed");
        }
    }
}

Hope it helped希望对您有所帮助

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

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