Godot Version
4.6
Question
I have some TouchScreenButton nodes being used for moving the character. I also have the game listen for InputEventScreenDrag to orient the camera. However, when a TouchScreenButton is pressed and the finger continues to move while it is pressed, the orientation of the camera moves along with it, which I don’t want to happen in this case. What makes this more of a problem is that when moving two or more fingers on the screen, the orientation of the camera moves seemingly unpredictably. How do I get controls similar to Minecraft’s controls on mobile such that pressing the movement buttons don’t move the camera?
Have the area of the screen you want draggable listen to the drag events instead of the whole game. Just put a Control node over where you want to listen and use _input() instead of physics_process()
1 Like
Thank you for the suggestion; I’ll try that right now.
That doesn’t seem to have worked; maybe I did something wrong?
I have a Control node in the scene tree with anchors preset set to “full rect”. It has this script.
extends Control
signal drag(x: float, y: float)
func _input(event: InputEvent) -> void:
if event is InputEventScreenDrag:
var screenDragEvent := event as InputEventScreenDrag
drag.emit(
screenDragEvent.screen_relative.x,
screenDragEvent.screen_relative.y
)
get_viewport().set_input_as_handled()
Then, in my player controller script, it listens for the signal.
func _on_background_touch_area_drag(x: float, y: float) -> void:
if movement_is_allowed:
yaw -= x * touch_sensitivity
pitch -= y * touch_sensitivity
clamp_pitch()
Yet, when I press a touch screen button and move my finger, it still affects the orientation of the camera.
This is what you did wrong. With anchors set to full rect, you are covering the entire screen, including the buttons. There are a number of ways you can fix this, including custom anchors, but for now try putting it inside a MarginContainer. Then set the Theme Overrides → Constants → Margin Bottom to like 200, or however many pixels will exclude your buttons.
1 Like
I did that, I put the Control node in a margin container, and set all the constants to 20, so it is just in the corner of the screen really small and doesn’t overlap with any touchscreen buttons.
Then, when I exported the game, I could still move the camera orientation from anywhere on the screen. I tried using _gui_input instead of _input.
When I have time, I will start up a fresh Godot project and make a minimal example of what I am trying to do, then share it here, so that hopefully it is more obvious what I am doing incorrectly.
1 Like
Paste the full code for the Control here. Use Ctrl+E to format it.
Actually, you know what? Try doing the same thing with an Area2D as well. See if that works better.
Here is the full script for the Control node.
extends Control
signal drag(x: float, y: float)
func _gui_input(event: InputEvent) -> void:
if event is InputEventScreenDrag:
var screenDragEvent := event as InputEventScreenDrag
drag.emit(
screenDragEvent.screen_relative.x,
screenDragEvent.screen_relative.y
)
get_viewport().set_input_as_handled()
Is there a reason you’re using _gui_input() instead of _input()?
I don’t remember what my thought process was for using _gui_input.
I was probably just trying different things to improve my understanding and get it to work.
I’m returning to tinker with this problem again after having taken a break for several days.
I remember looking at this image from the documentation and trying to reason about it.
1 Like