0
\$\begingroup\$

I use Unity 2019.4. I made a script so that if a make a long press on the screen of my mobile phone, my Personnal Character stops. I created a button to shoot gun too. The problem is when I make long press on that button too (shoot button), my personal character also stops. How to avoid this and use "Shoot button" only for shooting.

        if (MobileInput.Instance.LongPress) 
        {
            {
                speed = Mathf.Lerp(speed, 0, 1f);
                if (speed <= 0)
                {
                    speed = 0;
                    isIdle = true;
                    isRunning = false;
                }
            }
        }
\$\endgroup\$
5
  • \$\begingroup\$ Is adding a bool to check if you are currently firing an option for you and just check on the long press if you are not firing? if (MobileInput.Instance.LongPress && !isFiring) \$\endgroup\$
    – Zibelas
    Feb 3, 2021 at 9:18
  • 1
    \$\begingroup\$ Did you check out past Q&A on similar topics, like UI clicks hitting game objects velow,, How to block interaction below panel, etc? A common solution recommended in these cases is using EventSystem.IsPointerOverGameObject to check if the touch is on a button and disregard it. \$\endgroup\$
    – DMGregory
    Feb 3, 2021 at 12:28
  • \$\begingroup\$ I will. Thanks! \$\endgroup\$
    – Achie1
    Feb 3, 2021 at 13:48
  • \$\begingroup\$ If an answer works, remember to come back and hit the check mark, so others know it worked for you. \$\endgroup\$ Feb 3, 2021 at 17:12
  • \$\begingroup\$ What I was looking for is what DMGregory suggested. It is how to BLOCK interaction below panel. I did not express it correctly in the begining. \$\endgroup\$
    – Achie1
    Feb 4, 2021 at 5:50

2 Answers 2

1
\$\begingroup\$

This is how I fixed it thanks to suggestions I received.
In my Input script(where Mobile and Keyboard Inputs are handled),I inserted "EventSystem.IsPointerOverGameObject()". I removed Image component in the UI Panel coz Image component can be transparent so invisible but it can block interaction with the screen (taps and swipes on the screen).

    if (Input.GetMouseButtonDown(0))
    {
        if (EventSystem.current.IsPointerOverGameObject())
            return;
            
        tap = true;
        startTouch = Input.mousePosition;
    }
\$\endgroup\$
0
\$\begingroup\$

As Zibelas said in the comments, you could use an isShooting bool to check whether or not you are firing, and thus prevent the player from stopping when the input happens to be a longpress. Here's the script:

public bool isShooting;

        if (MobileInput.Instance.LongPress && !isShooting) 
        {
            {
                speed = Mathf.Lerp(speed, 0, 1f);
                if (speed <= 0)
                {
                    speed = 0;
                    isIdle = true;
                    isRunning = false;
                }
            }
        }
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .