Skip to content
This repository has been archived by the owner on Jan 5, 2022. It is now read-only.

Commit

Permalink
Fixing a bug on Windows where GetButtonUp will trigger when the game …
Browse files Browse the repository at this point in the history
…starts (Issue 27)
  • Loading branch information
Jibran Syed committed Apr 15, 2017
1 parent b7dbcdb commit 4da5d42
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1,114 deletions.
75 changes: 60 additions & 15 deletions XboxCtrlrInput/Assets/Plugins/XboxCtrlrInput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,17 +1031,6 @@ public static bool IsPluggedIn(int controllerNumber)
// ------------- Private -------------- //
////

// ------------ Members --------------- //

// Windows only subsystem
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
private static GamePadState[] xInputCtrlrs = new GamePadState[4];
private static GamePadState[] xInputCtrlrsPrev = new GamePadState[4];
private static int xiPrevFrameCount = -1;
private static bool xiUpdateAlreadyCalled = false;
private static bool xiNumOfCtrlrsQueried = false;
#endif

// ------------ Methods --------------- //

private static bool OnMac()
Expand Down Expand Up @@ -1578,14 +1567,69 @@ private static string DetermineDPadWirelessLinux(XboxDPad padDir, int ctrlrNum)
// ------------- Private XInput Wrappers (for Windows Native player and editor only) -------------- //

#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN

// ------------ Inner Class ------------//

// Used to setup XInput for Windows
class XciXInputInitializer
{
// Ctor
public XciXInputInitializer()
{
// Only runs once.
// Setup XInput here...

//>> For updating states <<

// Call input states twice in order to make sure that
// the previous states and current states are the same.
// This is needed to prevent errors where GetButtonUp will trigger unexpectledly

for (int i = 0; i < 4; i++)
{
PlayerIndex plyNum = (PlayerIndex)i;
xInputCtrlrsPrev[i] = xInputCtrlrs[i];
xInputCtrlrs[i] = GamePad.GetState(plyNum, XCI.WindowsDeadzoneMethod);
}

for (int i = 0; i < 4; i++)
{
PlayerIndex plyNum = (PlayerIndex)i;
xInputCtrlrsPrev[i] = xInputCtrlrs[i];
xInputCtrlrs[i] = GamePad.GetState(plyNum, XCI.WindowsDeadzoneMethod);
}

}

public void Dummy()
{
// Intentionally does nothing to prevent warnings
}
}



// ------------ Members --------------- //

// Windows variables
private static GamePadState[] xInputCtrlrs = new GamePadState[4];
private static GamePadState[] xInputCtrlrsPrev = new GamePadState[4];
private static int xiPrevFrameCount = -1;
private static bool xiUpdateAlreadyCalled = false;
private static bool xiNumOfCtrlrsQueried = false;
private static XciXInputInitializer xinputInitalizer = new XCI.XciXInputInitializer();



// This can be set to something different if you want
private const GamePadDeadZone WindowsDeadzoneMethod = GamePadDeadZone.Circular;


private static void XInputUpdateAllStates()

// ------------ Methods --------------- //

//>> For updating states <<

private static void XInputUpdateAllStates()
{
if(xiUpdateAlreadyCalled) return;

Expand Down Expand Up @@ -1695,7 +1739,8 @@ private static float XInputGetAxisState(GamePadThumbSticks xiThumbSticks, XboxAx
private static bool XInputStillInCurrFrame()
{
bool r = false;

xinputInitalizer.Dummy(); // Stop warnings about not using the initilaizer

// Get the current frame
int currFrame = Time.frameCount;

Expand All @@ -1719,7 +1764,7 @@ private static bool XInputStillInCurrFrame()

#endif

// END of Windows only subsystem
// END of Windows only subsystem ==========================================



Expand Down
12 changes: 7 additions & 5 deletions XboxCtrlrInput/Assets/XboxCtrlrInputExample/MovePlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ void Start ()


newPosition = transform.position;
//*


// Call for the number of connected controllers once
if(!didQueryNumOfCtrlrs)
{
didQueryNumOfCtrlrs = true;

int queriedNumberOfCtrlrs = 1;//XCI.GetNumPluggedCtrlrs();
int queriedNumberOfCtrlrs = XCI.GetNumPluggedCtrlrs();

if(queriedNumberOfCtrlrs == 1)
{
Expand All @@ -70,7 +72,7 @@ void Start ()

XCI.DEBUG_LogControllerNames();
}
//*/

}


Expand All @@ -80,14 +82,14 @@ void Update ()
GameObject bulletReference = null;

// Jump (Left Stick)
if(XCI.GetButtonUp(XboxButton.LeftStick, controller) && canJump)
if(XCI.GetButtonDown(XboxButton.LeftStick, controller) && canJump)
{
canJump = false;
GetComponent<Rigidbody>().AddRelativeForce(0.0f, jumpImpulse, 0.0f, ForceMode.Impulse);
}

// Slam (Right Stick)
if(XCI.GetButtonUp(XboxButton.RightStick, controller) && !canJump)
if(XCI.GetButtonDown(XboxButton.RightStick, controller) && !canJump)
{
GetComponent<Rigidbody>().AddRelativeForce(0.0f, (-jumpImpulse * 1.5f), 0.0f, ForceMode.Impulse);
}
Expand Down
Loading

0 comments on commit 4da5d42

Please sign in to comment.