0
\$\begingroup\$

My character jumps when I press the right side of screen. The pause button is alsp in the top right corner. So when I press that button, the right side script always triggers and I don't want that to happen.

Here the script :

 if (Input.GetMouseButtonDown(0))
            {
                float xPosition = Input.mousePosition.x;

                if (xPosition > Screen.width / 2 && isGrounded && !isJump)
                {
                    // mouse is on the right part of the screen, shoot right

                    rb2d.velocity = new Vector3(0, jumpForce, 0);

                    isJump = true;

                    JumpSoundSfx();
                }
            }

            if (Input.GetMouseButton(0))
            {
                float xPosition = Input.mousePosition.x;

                if (xPosition < Screen.width / 2 && !isJump)
                {
                    // mouse is on the left part of the screen, so shoot to the left

                    crouch.SetActive(true);

                    stand.SetActive(false);
                }
            }
```
\$\endgroup\$
3
  • \$\begingroup\$ Would it not be a better solution to not shoot when you are over your pause button? On different resolutions it could be on different positions (higher/ lower or the button just be bigger) \$\endgroup\$
    – Zibelas
    Commented Feb 6, 2021 at 12:50
  • \$\begingroup\$ @Zibelas yeah, actually i want like that by addding some bool but its same. noting changed. \$\endgroup\$ Commented Feb 6, 2021 at 14:50
  • 1
    \$\begingroup\$ I just searched for a few things that might give some pointers for you. It's also possible to detect when your mouse is over a button (answers.unity.com/questions/783279/…). That link is a bit old though so it might have become more easier in the past few years. If you know when your mouse hovers over the button, you can flag a bool cantJump for example, then apply that to your code. \$\endgroup\$
    – D.Kallan
    Commented Feb 9, 2021 at 7:47

2 Answers 2

1
\$\begingroup\$

Unity has a way to “consume” input events, so that they don’t trickle down to other places that listen to them. This is a somewhat complicated topic, but generally, the ones rendered “on top” will get the events first. So if you make them eat up the event, it won’t reach the lower layers “underneath”.

Check out https://docs.unity3d.com/ScriptReference/Event.Use.html

Good place to start.

\$\endgroup\$
2
\$\begingroup\$

Yeah, actually I've fixed the issue using this EventSystems.current.IsPointerOverGameObject(), here the full doc

https://docs.unity3d.com/2018.2/Documentation/ScriptReference/EventSystems.EventSystem.IsPointerOverGameObject.html

 if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
            {
                float xPosition = Input.mousePosition.x;

                if (xPosition > Screen.width / 2 && isGrounded && !isJump)
                {
                    // mouse is on the right part of the screen, shoot right

                    rb2d.velocity = new Vector3(0, jumpForce, 0);

                    isJump = true;

                    JumpSoundSfx();
                }
            }

            if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
            {
                float xPosition = Input.mousePosition.x;

                if (xPosition < Screen.width / 2 && !isJump)
                {
                    // mouse is on the left part of the screen, so shoot to the left

                    crouch.SetActive(true);

                    stand.SetActive(false);
                }
            }
\$\endgroup\$
1
  • \$\begingroup\$ If you used this to fix your problem, shouldn't that be marked as the answer then? \$\endgroup\$
    – D.Kallan
    Commented Feb 9, 2021 at 18:33

You must log in to answer this question.

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