Player character jumping when I wish to only initiate pause - UI Button element.

Hey,

So I’m developing a platformer that uses 'Input.GetMouseButtonDown’ in order to allow my player character to jump.

I’ve since started developing a pause mechanic for my game which in essence successfully works. Unfortunately, it fails to work perfectly with the Button UI element. When playing the game in the editor and tapping the button UI element - my game will successfully pause however my player will also jump. I’ve tried replacing the self-implemented “On Click()” function with ‘Event Triggers’ as well as an assortment of other attempted fixes.

I was thinking of using a coroutine in order to slightly delay the input so that a bool or int may ring true before the jump is initiated… but surely there’s a more practical solution; a more convenient way around the conundrum of the UI element using a duplicate input of that used in the game?

There’s something simple I’m missing surely?

Thanks.

1 Answer

1

You could think that using a button to do gameplay action and UI action is wrong design. The simplest way is to change the button for jump, use the second mouse button or a keyboard button.

If you need to keep that first button for everything, in the jump check that you are not in the UI area:

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

The example is pretty much your solution.

Thanks for pointing me in the right direction - I ultimately decided on using 'OnPointerEnter' and 'OnPointerExit' for my solution. Thanks.