简体   繁体   中英

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:

在此处输入图像描述

Vertical1 is used to Throttle function and Vertical2 is used to Break function. This configuration are working now.

Now I need to configure also another controller (HC1 3DRap). This is an Hand Controller. So I checked it on windows device and I can see this:

在此处输入图像描述

Rotation Axis X and Rotation Axis Y have a value in sleep mode (without press the two levels).

Now I need to integrate also this new Controller in my project. 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):

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.

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. When you use the POV-Stick you might be moving the whole joystick and therefore change the main axis of the joystick. If you are just watching the main axis input, it looks like that is the input of your POV-Stick, but actually isn't. 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. If you try to handle that on your own, you go crazy.

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:

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.

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. 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.:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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