Clicks on ui buttons vs world

hi all,
i am using this function to get where a player has clicked and then move the character accordingly.
if (Input.GetKeyDown(KeyCode.Mouse0))
{
rubyPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}

I have added a ui button for shooting. This button is on screen space. Unfortunately my movement is triggered by the button too.

Is there an elegant way or best practice to move by click but not when on an ui button?

Thank you

Could check for mouse cursor hovering over the button and if so flip a boolean true and if not false; then the world space object checks and if true they abort prior to executing movement code.

Wrong forum. This is a UI question… proper place would be scripting or UGUI forum.

“Is there an elegant way or best practice to move by click but not when on an ui button?”

You can detect if your click happens over a UI element, there are several ways to do this.

One way to detect if you are over a UI element is to use this: EventSystem.current.IsPointerOverGameObject()

So if the last click was over a UI element, you could ignore your player movement input.

1 Like

Hey man you just saved me from my headache! Thank you!

1 Like