3
\$\begingroup\$

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:

Horizontal axis settings

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.

\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Daniel Sep 23 '15 at 21:39
  • \$\begingroup\$ Yes it is called from the Update method. I'll retest it with another axis, thanks. \$\endgroup\$ – NDraskovic Sep 24 '15 at 7:20
  • \$\begingroup\$ Hey, I changed the axis, but it still isn't working. \$\endgroup\$ – NDraskovic Sep 24 '15 at 15:46
  • \$\begingroup\$ 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. \$\endgroup\$ – Daniel Sep 24 '15 at 19:58
  • \$\begingroup\$ 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. \$\endgroup\$ – NDraskovic Sep 25 '15 at 12:34
1
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.