Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wasplay · Nov 01, 2018 at 11:23 PM 0
Share

bump ?.....

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

110 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[New Player Input System] Separate players in scene 0 Answers

Keyboard and Controller Input Not Working in WebGL 1 Answer

Unity input system: TapInteraction does not produce a value when using ReadValue() 0 Answers

How do I have multiple controls for the same function? (Controller and Keyboard) 1 Answer

Why does the keyboard control what button is selected? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges