1

I'm writing a script to put on a button that will detect a drag direction to move a player

void OnGUI()
{
    if (buttonRect.Contains(Event.current.mousePosition))
    {
        if (Event.current.type == EventType.MouseDown)
        {
            buttonPressed = true;
        }
        if (Event.current.type == EventType.MouseUp)
        {
            buttonPressed = false;
        }
    }
    if (buttonPressed && Event.current.type == EventType.MouseDrag)
    {

    }
}

If this script was to be placed on a button, how could I get the buttons bounds as a rectangle?

Also, If anyone has a better solution for controling movement by drag I would be open for suggestions.

Programmer
  • 121,791
  • 22
  • 236
  • 328
johnny 5
  • 19,893
  • 50
  • 121
  • 195
  • 3
    OnGui should not be used for new development. It is only still there for debugging purposes and backward compatability in modern editions of unity. From [the documentation](https://docs.unity3d.com/Manual/GUIScriptingGuide.html): *"The IMGUI system is not generally intended to be used for normal in-game user interfaces that players might use and interact with"* – Scott Chamberlain Jun 04 '16 at 01:13
  • @ScottChamberlain thanks for the heads up. Do you know how I could implement drag? – johnny 5 Jun 04 '16 at 01:20
  • Not sure if it covers dragging, but here is [the tutorial on Button](https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-button?playlist=17111) for the modern UI system. – Scott Chamberlain Jun 04 '16 at 01:22
  • Thanks I'll check this out – johnny 5 Jun 04 '16 at 01:24
  • Are trying to make a visual joystick? Please explain more of what your are trying to do – Programmer Jun 04 '16 at 03:58
  • Yeah I was trying to make a joystick – johnny 5 Jun 04 '16 at 04:00
  • you **must not** use the old "ongui" system, it is now deprecated and does not work. (Just use UI for your buttons etc - it is ridiculously easy. Just click in the menu "add canvas" and then click "add button".) Regarding your question it is a one billion times duplicate! – Fattie Jun 04 '16 at 13:15

2 Answers2

3

You can implement your own Visual Joystick by using the Unity new event callback functions such as OnBeginDrag OnDrag and the OnEndDrag function. There is a already made Visual Joystick package out there for Unity, so implementing your own is like reinventing the wheel.

All you have to do is to import the CrossPlatformInputManager package from Unity's UnityStandardAssets then use CrossPlatformInputManager.GetAxis("Horizontal") and CrossPlatformInputManager.GetAxisRaw("Horizontal") to read the direction of the image/thumb.

To make one from scratch, you can flow this video tutorial.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • If you have time do you, know how to implement a cross playform swipe/drag across the joystick without initially clicking on it? – johnny 5 Jun 04 '16 at 15:58
0

If you want a mobile solution that is really accurate and natural like a physical joystick, take a look at this asset:

Ultra Precise D-Pad Joystick

  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 13 '21 at 00:04