- Home /
Game doesn't recognize X-axis input from XBox 360 controller
I have a problem with the usage of XBox controller in Unity game. I connected the controller to the PC and it works fine, I also managed to use it in the game itself - I'm using "A" button to fire and Start to start the game and they work properly. However the game doesn't recognize the x-axis movement (y-axis would probably have the same problem but we don't use it in our game). I'm not sure if the problem is in the input settings or my code. This is how the Horizontal movement is defined in the game:
This is the code that handles the movement:
float x = Input.GetAxis("Horizontal");
Vector3 newPosition = new Vector3(transform.position.x + x*playerXMoveSpeed, 0, 0);
newPosition.y = transform.position.y;
newPosition.z = 0;
transform.position = Vector3.Lerp(transform.position, newPosition, playerXMoveSpeed * Time.deltaTime);
var limit = animator.SpriteRenderer.bounds.extents;
if (transform.position.x - limit.x < leftBorder)
{
var capped = transform.position;
capped.x = leftBorder + limit.x;
transform.position = capped;
}
else if (transform.position.x + limit.x > rightBorder)
{
var capped = transform.position;
capped.x = rightBorder - limit.x;
transform.position = capped;
}
OnMove(gameObject);
All variables are more or less self explanatory, the OnMove is a delegate that just applies the transform to the gameObject in question. The entire code is inside a void method called Move(). That method is called inside an if statement:
if ((Input.GetAxis("Horizontal") != 0.0f || Input.GetButtonDown("Horizontal")))
{
Move();
}
Like I said, the game recognizes the other buttons, but not the x-axis, it doesn't move the character. The Input.GetButtonDown is there just so keyboard input would be supported also (and it works properly). Can you see the problem with this code or settings? Please advise.
Just a quick update - when I switch to the 4th axis (the right side joystick) it works as it should, why is this happening?
http://wiki.unity3d.com/index.php?title=Xbox360Controller
$$anonymous$$aybe that will help some.
Yeah, that's what I used in the first place, and like I said everything except the x-axis works, 4th axis works perfectly, key mappings like "A"=0 and "Start"=7
Answer by Kornikolia · Sep 28, 2015 at 07:51 PM
Maybe it will be easy for you to use UnityStandardAssets.CrossPlatformInput ?
Honestly, never heard of it. Thanks, I'll give it a look.