0
\$\begingroup\$

I am currently trying to put two buttons on my UI that move a character left and right if touched down (Held). I would like to use the cross platform input, and would like the buttons to change the value of the Horizontal axis as my "A" and "D" keys would...

enter image description here

And my code for moving my character is as follows:

private void Update()
    {
        isGrounded = Physics2D.OverlapCircle(groundChecker.position, .15f, groundLayer);


        float move = CrossPlatformInputManager.GetAxis("Horizontal") * moveSpeed;
        playerRB.velocity = new Vector2(move, playerRB.velocity.y);




    }
\$\endgroup\$
2
  • \$\begingroup\$ Do you mean to use the OnDrag(PointerEventData data) event to move the character? \$\endgroup\$ – NorbyAriel Jan 5 '18 at 12:06
  • \$\begingroup\$ No I want the buttons to link to the Input Horizontal Axis like the A and D keys do when building for standalone \$\endgroup\$ – cipherous Jan 5 '18 at 18:28
0
\$\begingroup\$

Try to check few things:

1) Canvas (Root) with buttons must have own higher order layer (sorting layers)
2) Buttons must have Z axis enough close to the camera
3) In Event trigger you should:

  • use trigger Pointer Down and ButtonHandler.SetAxisNegativeState (for the left)
  • use trigger Pointer UP and ButtonHandler.SetAxisNeutralState (for the left)

  • use trigger Pointer Down and ButtonHandler.SetAxisPositiveState (for the right)

  • use trigger Pointer Up and ButtonHandler.SetAxisNeutralState (for the right)

4) Maybe you will need add a Graphic Raycaster for parent (of buttons) canvas

I had same issue today and I think it is also in your case, buttons was just un-clickable.

Cheers

\$\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.