0

I am trying to extend cross platform input in order to make mobile buttons to move a character left and right. What I did was I created two buttons and a script attached to each called MobileMovementButtons.cs. I run the program and the character moves left just fine but not right... I think it has something to do with my Conditional statement..

Here is my Code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityEngine.EventSystems; 

namespace CrossPlatformInput {
    public class MobileMovementButton : MonoBehaviour, IPointerUpHandler, IPointerDownHandler{

        public enum Direction {
            Left,
            Right
        }

        public Direction buttonDirection = Direction.Left;

        CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis;


        private bool rightPressed = false;
        private bool leftPressed = false;


        void Awake() {
            CrossPlatformInputManager.UnRegisterVirtualAxis ("Horizontal");
        }

        void OnEnable() {
            m_HorizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis("Horizontal" );
            CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
        }

        void Update() {
            if (rightPressed) {
                m_HorizontalVirtualAxis.Update (1);
            }
            if (leftPressed) {
                m_HorizontalVirtualAxis.Update (-1);
            } 
            if (!leftPressed && !rightPressed){
                m_HorizontalVirtualAxis.Update (0);
            }
        }

        public void OnPointerDown(PointerEventData eD) {
        if (this.buttonDirection == Direction.Left) {
            leftPressed = true;
        } 

        if (this.buttonDirection == Direction.Right) {
            rightPressed = true;
        }
    }

    public void OnPointerUp(PointerEventData eD) {
        if (this.buttonDirection == Direction.Left) {
            leftPressed = false;
        } 

        if (this.buttonDirection == Direction.Right) {
            rightPressed = false;
        }
    }


    }
}

And here is the buttons:

enter image description here

Wp3Dev
  • 2,001
  • 7
  • 38
  • 54
  • Try passing plain `true` and `false` on their respective Up/Down events. – Brandon Miller Dec 31 '17 at 19:26
  • I just tried that, updated above, doesn't work. – Wp3Dev Dec 31 '17 at 19:29
  • OH, and `buttonDirection` is never being assigned. You need to access the PointerEventData and actually see which button is being pressed, and assign `buttonDirection` accordingly. – Brandon Miller Dec 31 '17 at 19:29
  • I assigned it in the Identity Inspector on Unity. I just ran a Log: Debug.Log (this.buttonDirection == Direction.Right); and it shows that it is working – Wp3Dev Dec 31 '17 at 19:30
  • Is there any reason you aren't using the pre-made EventTrigger component? `Component > Event > EventTrigger` – Brandon Miller Dec 31 '17 at 19:44
  • @BrandonMiller Avoid using EventTrigger if possible. It is slow when responding to touch events. – Programmer Jan 01 '18 at 00:07
  • @Programmer what do you recommend in performing this task? I Solved it using the event trigger but I am thinking I may just build my own input manager that handles touch events and calls player functions... – Wp3Dev Jan 01 '18 at 18:19
  • See [this](https://stackoverflow.com/questions/41391708/how-to-detect-click-touch-events-on-ui-and-gameobjects/41392130#41392130). – Programmer Jan 01 '18 at 18:58

0 Answers0