# Using XBox 360 controller in Unity game - game doesn't recognize axis movement

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. 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")) && !IsContinueDisplayed)
{
FollowTouch();
}


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.

• I tested this myself and it worked just fine. I would suggest trying the other axis types to make help sure it's not your controller. Try changing the axis to "Y" for vertical, "axis 4" for the right stick horizontal, and "axis 5" for the right stick vertical. I'm assuming you have this in the FixedUpdate or Update events or this would never work. – Daniel Sep 23 '15 at 21:39
• Yes it is called from the Update method. I'll retest it with another axis, thanks. – NDraskovic Sep 24 '15 at 7:20
• Hey, I changed the axis, but it still isn't working. – NDraskovic Sep 24 '15 at 15:46
• Are you sure your algorithm is working? If not I would replace the "x" variable with a number between -1 and 1 to see if it moves. – Daniel Sep 24 '15 at 19:58
• The same algorithm works when the keyboard is used (a bit twitchy but it moves the character as it should). The biggest problem is that this code is originally intended for mobile phones (and it already works on Android), we are just making a PC version for promotion purposes. So the algorithm is created for touch input, I just changed it so that it would accept float instead of Vector3 input. – NDraskovic Sep 25 '15 at 12:34

## 1 Answer

I tested this myself and it worked just fine. I would suggest trying the other axis types to make help sure it's not your controller. Try changing the axis to "Y" for vertical, "axis 4" for the right stick horizontal, and "axis 5" for the right stick vertical. I'm assuming you have this in the FixedUpdate or Update events or this would never work.