0
\$\begingroup\$

I am building a Roulette game in Unity 5. I want to place bets on a Roulette table when I touch specific areas on the screen with mouse(PC) and touch(mobile).

For example when i touch Number 1 on the roulette table a bet should be placed on that number. I tried using box collider iwth trigger but I think thats for only colliders.

Screenshot attached

Screenshot of my Roulette table numbers

\$\endgroup\$
  • \$\begingroup\$ Can I use panels to do that \$\endgroup\$ – Jayesh Duggal Feb 26 '17 at 3:43
1
\$\begingroup\$

you're on the right way. now you should create a class that will listen to any clicks on the roulette table, and in the update function of that class you should get the location of the click (either touch or mouse click) with raycast, just attach the following script to an empty gameobject on the hierarchy and you're good to go. i would also recommend tagging the roulette numbers with certain tag like "rouletteNumber".

private InputMechanism _inputMechanism = new MouseInput { 0 };
// 0 - mouse left click
// 1 - right click
// 2 - middle click

void Start()
 {
       // change input mechanism according to platform
       if (Application.isMobilePlatform)
       {
             _inputMechanism = new TouchInputSingle();
       }
} 

void Update()
{
    if (_inputMechanism.IsDown())
    {
        position = _inputMechanism.GetScreenPosition();
        Ray ray = Camera.main.ScreenPointToRay(position);
        RaycastHit2D hit = Physics2D.GetRayIntersection(ray, Mathf.Infinity);

        if (hit.collider != null && (hit.collider.tag == "rouletteNumber"))
        {
            // do whatever you want to do here
        }
    }

}

// Mouse input class

public class MouseInput : InputMechanism
    {
        public int Button = Globals.Mouse.Buttons.LEFT;

        #region Overrides of InputMechanism
        public override bool IsDown()
        {
            return Input.GetMouseButtonDown(Button);
        }

        public override bool IsUp()
        {
            return Input.GetMouseButtonUp(Button);
        }

        public override Vector2 GetScreenPosition()
        {
            return Input.mousePosition;
        }
        #endregion
    }

// touch input class

public class TouchInputSingle : InputMechanism
    {
        private int _trackedTouchId = -1;
        private Vector2 _touchPosition;

        #region Overrides of InputMechanism
        public override bool IsDown()
        {
            if (_trackedTouchId < 0)
            {
                for (int i = 0; i < Input.touchCount; i++)
                {
                    if (Input.GetTouch(i).phase == TouchPhase.Began)
                    {
                        _trackedTouchId = i;
                        return true;
                    }
                }
            }

            return false;
        }

        public override bool IsUp()
        {
            if (_trackedTouchId >= 0)
            {
                switch (Input.GetTouch(_trackedTouchId).phase)
                {
                    case TouchPhase.Ended:
                    case TouchPhase.Canceled:
                        _trackedTouchId = -1;
                        return true;
                }
            }

            return false;
        }

        public override Vector2 GetScreenPosition()
        {
            if (_trackedTouchId >= 0)
            {
                _touchPosition = Input.GetTouch(_trackedTouchId).position;
            }

            return _touchPosition;
        }
        #endregion

// abstract input mechanism

public abstract class InputMechanism
    {
        public abstract bool IsDown();
        public abstract bool IsUp();
        public abstract Vector2 GetScreenPosition();
    }
\$\endgroup\$
  • \$\begingroup\$ Thanks for your help. Will it be possible to use Panel with Sub Panels in it with unity Event Trigger and Event system work. I am right now testing on PC with mouse clicks. If I use Panels will I be able to use it on mobile with touch input. \$\endgroup\$ – Jayesh Duggal Feb 26 '17 at 8:17
  • \$\begingroup\$ yes. it will work with event triggers as well \$\endgroup\$ – Ido Ben Shalom Feb 26 '17 at 8:21
0
\$\begingroup\$

You can create a component that implements these interfaces IPointerClickHandler , IPointerDownHandler, IPointerUpHandler. This way you can can handle input with both the mouse and touch. Your class should look something like this.

public class MyInputClass : MonoBehaviour, IPointerClickHandler , IPointerDownHandler, IPointerUpHandler
{

    public void OnPointerClick(PointerEventData eventData)
    {
      Debug.log("We clicked on: "+gameObject.name);
    }

    public void OnPointerDown(PointerEventData eventData)
    {
       // throw new NotImplementedException();
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        //throw new NotImplementedException();
    }


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