Currently, my game uses a virtual joystick for movement, touch input for camera rotation, and buttons for jumping and shooting. The problem is that if the player is using the joystick to move forward, they can’t rotate the camera or press any buttons. How can I fix this? I’ve tried numerous approaches, but none have worked. For reference, I’m using “TextureButton” for the buttons and the “Virtual Joystick” asset from the “AssetLib” for the joystick. Thanks in advance!
First, use TouchScreenButton
for jumping and shooting button.
Thanks! And how can I fix the camera rotation?
Can you show the codes.
Yes, I can! How would it be more convenient for you to familiarize yourself with the code?
Just paste your codes here in proper format like this:
``` I used this symbol for this code format ```
Only the camera movement codes
``` func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseMotion :
rotate_y(-event.relative.x * look_sensitivity)
%Head.rotate_x(-event.relative.y * look_sensitivity)
%Camera3D.rotation.x = clamp(%Camera3D.rotation.x, deg_to_rad(-90), deg_to_rad(90))
var target_recoil := Vector2.ZERO
var current_recoil := Vector2.ZERO
const RECOIL_APPLY_SPEED : float = 10.0
const RECOIL_RECOVER_SPEED : float = 7.0
func add_recoil(pitch: float, yaw: float) -> void:
target_recoil.x += pitch
target_recoil.y += yaw
func update_recoil(delta: float) -> void:
# Slowly move target recoil back to 0,0
target_recoil = target_recoil.lerp(Vector2.ZERO, RECOIL_RECOVER_SPEED * delta)
# Slowly move current recoil to the target recoil
var prev_recoil = current_recoil
current_recoil = current_recoil.lerp(target_recoil, RECOIL_APPLY_SPEED * delta)
var recoil_difference = current_recoil - prev_recoil
# Rotate player/camera to current recoil
rotate_y(recoil_difference.y)
%Camera3D.rotate_x(recoil_difference.x)
%Camera3D.rotation.x = clamp(%Camera3D.rotation.x, deg_to_rad(-90), deg_to_rad(90))
#%ThirdPersonOrbitCamPitch.rotation.x = %Camera3D.rotation.x
func _head_effect(delta):
head_time += delta * self.velocity.length()
%Camera3D.transform.origin = Vector3(
cos(head_time * HEAD_FREQUENCY * 0.5 ) * HEAD_MOVE_AMOUNT,
sin(head_time * HEAD_FREQUENCY ) * HEAD_MOVE_AMOUNT,
0
) ```
First, try to use _input
function instead of _unhandled_input
, if it does not works, use InputEventScreenDrag
instead of InputEventMouseMotion
Using _input() partially works, but now there are cases when the camera rotates non-stop when you press the character control buttons. How can I fix this?
I made a first person controller a long ago (about 1-2 yrs ago), and I can clearly remember that I also stuck in a problem like that but somehow fixed it… But unfortunately I lost the project files, so I can’t tell you how to fix it currently. But as far as I can remember, I used a joystick addon from assets library, touch screen buttons for jump and shoot and InputEventMouseMotion…
As a last try, you can try to adjust the settings of mouse pointing (in project settings, just search pointing).
You can also look for a tutorial on it or check a similar project in assets library.