flutter-unity-view-widget icon indicating copy to clipboard operation
flutter-unity-view-widget copied to clipboard

Unity's OnMouseDown function not working

Open cancaglar opened this issue 2 years ago • 4 comments

This function is not working when I use this flutter package image

cancaglar avatar Jun 06 '23 07:06 cancaglar

Which platform is this?

timbotimbo avatar Jun 06 '23 09:06 timbotimbo

Hi @cancaglar ,

Don't use the method OnMouseDown() on phones, this will not work on all devices also you can get a lot of errors, for tapping interaction use in Update() --- >Input.touch

        void Update()
        {
            if (Input.touchCount > 0)
            {
                m_touch = Input.GetTouch(0);
                //logic on Tap
                print("TAP");

                if (m_touch.phase == TouchPhase.Began)
                {
                    var hit = new RaycastHit();

                    Ray ray = m_mainCamera.ScreenPointToRay(Input.GetTouch(0).position);


                    if (Physics.Raycast(ray, out hit, m_maxDistanceHit))
                    {
                        Debug.Log(hit.transform.gameObject.tag);
                        if (hit.transform.gameObject.tag == "PLAYER")
                        {
                            Debug.Log("SOME ACTION ON TAPPING ON PLAYER");
                        }
                        UnityMessageManager.Instance.SendMessageToFlutter($"action#{hit.transform.gameObject.tag}");
                    }
                }

            }
        }

RoyalCoder88 avatar Jun 06 '23 14:06 RoyalCoder88

Which platform is this?

android

cancaglar avatar Jun 07 '23 09:06 cancaglar

Hi @cancaglar ,

Don't use the method OnMouseDown() on phones, this will not work on all devices also you can get a lot of errors, for tapping interaction use in Update() --- >Input.touch

        void Update()
        {
            if (Input.touchCount > 0)
            {
                m_touch = Input.GetTouch(0);
                //logic on Tap
                print("TAP");

                if (m_touch.phase == TouchPhase.Began)
                {
                    var hit = new RaycastHit();

                    Ray ray = m_mainCamera.ScreenPointToRay(Input.GetTouch(0).position);


                    if (Physics.Raycast(ray, out hit, m_maxDistanceHit))
                    {
                        Debug.Log(hit.transform.gameObject.tag);
                        if (hit.transform.gameObject.tag == "PLAYER")
                        {
                            Debug.Log("SOME ACTION ON TAPPING ON PLAYER");
                        }
                        UnityMessageManager.Instance.SendMessageToFlutter($"action#{hit.transform.gameObject.tag}");
                    }
                }

            }
        }

Yes this is exactly what I did. But this is kind of weird issue.

cancaglar avatar Jun 07 '23 09:06 cancaglar