Reputation: 93
As the title suggests, I have connected a usb controller into unity of which worked perfectly. I mapped the specific keys to be used and its working without a hassle. However when I builded my game and run it, my controller doesn't work on it anymore. I tried editing the Inputs on the startup page of the game (where you can edit the graphics etc), I tried assigning a button on the inputs by double-clicking it, it doesn't read my controller's input no matter what button I press.
It's not my controller I think that's wrong since I can play other games using it. I tried FEZ and the moment I plugged my controller and started the game, the game immediately detected it and changed its controls from keyboard to controller. It works on Unity's Editor, but on the final build it doesn't even detect it.
How can I fix this?
Upvotes: 0
Views: 2536
Reputation: 1
I had the same problem, the reason it worked in the editor was that I did not enter directly from the main menu and the cursor was not locked. But after running the game build edit, I noticed that the cursor lock command in the new game button works because I entered the game from the main menu. After removing the cursor lock command on the new game button, the issue was resolved. I wanted to write that there may be a solution for those with such a problem.
Upvotes: 0
Reputation: 1596
Here follows a simple class for collecting some interesting data that you might use to debug your input, such as the name of currently connected joysticks, the values of the input axes, and keeps a log of the last 10 pressed keys. All data is collected in the Update()
method and is drawn to the top-left corner of the screen using OnGUI()
.
using System;
using System.Collections.Generic;
using UnityEngine;
public class RecordControllerInput : MonoBehaviour {
private string[] joystickNames;
private float xAxis, yAxis;
private List<string> lastPressedKeys = new List<string>();
void Update () {
joystickNames = Input.GetJoystickNames();
xAxis = Input.GetAxis("Horizontal");
yAxis = Input.GetAxis("Vertical");
foreach ( KeyCode curKey in Enum.GetValues(typeof(KeyCode)) )
{
if (Input.GetKeyDown(curKey))
{
lastPressedKeys.Add(curKey.ToString());
if (lastPressedKeys.Count > 10)
lastPressedKeys.RemoveAt(0);
}
}
}
private void OnGUI()
{
GUILayout.Label("Joysticks:");
foreach (var curName in joystickNames)
GUILayout.Label(string.Format(" {0}", curName));
GUILayout.Label(string.Format("Axes: ({0}, {1})", xAxis, yAxis));
GUILayout.Label("Last pressed keys:");
foreach (var curKeyName in lastPressedKeys)
GUILayout.Label(string.Format(" {0}", curKeyName));
}
}
Create an empty scene, add this script to any object (it can be the main camera if you want), and verify if the outputs align to your expectations (both in Unity Editor and in your built project). Here is a screenshot of the script running on my pc and picking up my connected Xbox USB Controller, the movements I made to the axes, and the key's I've pressed (both on my keyboard and on my controller).
If your usb controller is being detected by the engine, it should at least appear in the list that is being output. If that doesn't happen, it might indicate a bug on Unity or some sort of incompatibility with your specific USB controller, and in that case I'd suggest you to seek for more specialized help through their forums, or contact their support.
Upvotes: 1