- Home /
Question by
Wasplay · Nov 01, 2018 at 09:26 PM ·
inputkeyboard inputcontrollersinput manager
Best way to get controllers and keyboard input ?
I made this script using the XInput plugin for Unity :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XInputDotNetPure;
public class PlayersInputManager : MonoBehaviour {
private GameManager gm;
private GamePadState[] state = new GamePadState[4];
private PlayerIndex[] playerIndex = new PlayerIndex[4];
private bool buffer;
// Use this for initialization
void Start () {
gm = GetComponent<GameManager>();
for (int i = 0; i < 4; i++)
{
playerIndex[i] = (PlayerIndex)(i);
}
}
private void Update()
{
if (buffer) { GetInput(gm.buffer1); buffer = false; gm.inputs = gm.buffer1; gm.prevInputs = gm.buffer2; } else { GetInput(gm.buffer2); buffer = true; gm.inputs = gm.buffer2; gm.prevInputs = gm.buffer1; }
}
void GetInput(PlayerControls[] p) {
for (int i = 0; i < 4; i++)
{
state[i] = GamePad.GetState(playerIndex[i]);
if (state[i].IsConnected)
{
p[i].horizontal = state[i].ThumbSticks.Left.X;
p[i].vertical = state[i].ThumbSticks.Left.Y;
if (state[i].Buttons.Start == ButtonState.Pressed) { p[i].start = true; if (i == 3) { gm.keyboard = false; } } else { p[i].start = false; }
if (state[i].Buttons.LeftShoulder == ButtonState.Pressed) { p[i].jump = true; } else { p[i].jump = false; }
if (state[i].Buttons.RightShoulder == ButtonState.Pressed) { p[i].interact = true; } else { p[i].interact = false; }
if (state[i].Buttons.LeftStick == ButtonState.Pressed) { p[i].boost = true; } else { p[i].boost = false; }
if (state[i].Buttons.A == ButtonState.Pressed) { p[i].down = true; } else { p[i].down = false; }
if (state[i].Buttons.B == ButtonState.Pressed) { p[i].right = true; } else { p[i].right = false; }
if (state[i].Buttons.Y == ButtonState.Pressed) { p[i].up = true; } else { p[i].up = false; }
if (state[i].Buttons.X == ButtonState.Pressed) { p[i].left = true; } else { p[i].left = false; }
if (gm.vibrations) { GamePad.SetVibration(playerIndex[i], gm.vibration[i], gm.vibration[i]); }
}
}
if (Input.GetKeyDown(gm.start)) { gm.keyboard = true; }
if (gm.keyboard)
{
if (Input.GetKey(gm.moveleft)) { p[3].horizontal = -1f; }
else if (Input.GetKey(gm.moveright)) { p[3].horizontal = 1f; }else { p[3].horizontal = 0; }
if (Input.GetKey(gm.movedown)) { p[3].vertical = -1f; }
else if (Input.GetKey(gm.moveup)) { p[3].vertical = 1f; } else { p[3].vertical = 0; }
if (Input.GetKey(gm.shootdown)) { p[3].down = true; } else { p[3].down = false; }
if (Input.GetKey(gm.shootup)) { p[3].up = true; } else { p[3].up = false; }
if (Input.GetKey(gm.shootleft)) { p[3].left = true; } else { p[3].left = false; }
if (Input.GetKey(gm.shootright)) { p[3].right = true; } else { p[3].right = false; }
if (Input.GetKey(gm.take)) { p[3].interact = true; } else { p[3].interact = false; }
if (Input.GetKey(gm.boost)) { p[3].boost = true; } else { p[3].boost = false; }
if (Input.GetKey(gm.jump)) { p[3].jump = true; } else { p[3].jump = false; }
if (Input.GetKey(gm.start)) { p[3].start = true; } else { p[3].start = false; }
}
}
}
But I don't think it's the most optimized way to do it
I made that buffers system to make my own "ButtonDown" and "ButtonUp" functions
The Input Manager of Unity is really broken when you plug and unplug controllers, that's why I wanted to make my own system
Comment