0 votes

I developed an small game for phones and after some tests with TouchScreenButtons I noticed that pressing them doesnt consumes the event: I get a raycast in my floor too. How can I prevent the touch event going beyond the button and being interpreted as a touch in my floor? Maybe I need to place some invisible UI element under the button?

in Engine by (183 points)

2 Answers

+1 vote
Best answer

I got the same issue with raycasting and a button event, to solve this issue for me I created a timer into raycasting process so you can press a button and call a method to avoid raycasting event. As a raycasting happens first, I exposed a method for calling later from my GUI in this case a button event. The timer is just a pause to wait if it can avoid the raycasting.

I did these steps:

  1. Raycasting happens from a camera.
  2. I created a timer in a camera script this for doing a pause (I used 0.2 seconds).
  3. A button event can release over your GUI and later this calls a method from your camera script to avoid a raycasting with a flag variable.

I solved this issue for my 3D game and it works now. Also I am searching a better way to solve this issue more elegant.

by (188 points)
selected by

I found another solution and this is better than before one.

If you have a button on your screen you must use these two events.

  1. pressed()
  2. gui_input(event: InputEvent)

for your button of course.

func is_ui_event(event):
    if event is InputEventMouseMotion:
        return
    if event is InputEventScreenDrag:
        return
    if event is InputEventScreenTouch and event.is_pressed():
        get_tree().call_group("main_camera", "is_ui_event")
    if event is InputEventScreenTouch and not event.is_pressed():
        get_tree().call_group("main_camera", "is_not_ui_event")


func _on_btn_gui_input(event):
    is_ui_event(event)

My camera has this next code:

func _ready():
    add_to_group("main_camera")


func is_ui_event():
    ui_event = true


func is_not_ui_event():
    ui_event = false


func _input(event):
    if event is InputEventMouseMotion:
        return
    if event is InputEventScreenDrag:
        return
    if ui_event == false and event is InputEventScreenTouch and not event.is_pressed():
          # ALL YOUR CODE FOR RAYCASTING

If you have questions let me know.

+1 vote

Hi instead of input(event) method, make use of unhandledinput(event) method

by (189 points)

I moved the raycast code to unhandledinput and no raycast is made.

Thx, works like a charm.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.