class_name TwoSticksThirdPersonController extends Node signal on_update(subject_speed : float, subject_angle : float, camera_y_angle : float, camera_parameter : float) @export var dead_zone : float = 0.07 @export var camera_rotate_speed : float = PI @export var camera_var_speed : float = 1. @onready var left_stick : Vector2 = Vector2(0., 0.) @onready var right_stick : Vector2 = Vector2(0., 0.) @onready var update_direction : bool = false @onready var update_camera : bool = false @onready var y_angle : float = 0. @onready var other_value : float = 0. @onready var move_angle : float = 0. func _input(event : InputEvent) -> void: if event is InputEventJoypadMotion: var jev : InputEventJoypadMotion = event var value : float = jev.axis_value if abs(jev.axis_value) > dead_zone else 0. match jev.axis: JOY_AXIS_LEFT_X: left_stick.x = value JOY_AXIS_LEFT_Y: left_stick.y = value JOY_AXIS_RIGHT_X: right_stick.x = value JOY_AXIS_RIGHT_Y: right_stick.y = value _: pass update_direction = not (is_zero_approx(left_stick.x) and is_zero_approx(left_stick.y)) update_camera = not (is_zero_approx(right_stick.x) and is_zero_approx(right_stick.y)) func _process(delta : float) -> void: if update_direction or update_camera: y_angle += right_stick.x * camera_rotate_speed * delta if y_angle < 0: y_angle = 2 * PI + y_angle elif y_angle > 2 * PI: y_angle -= 2 * PI other_value = clampf( other_value - right_stick.y * camera_var_speed * delta, 0., 1.) var transformed_left : Vector2 = left_stick.rotated(y_angle) var move_speed : float = transformed_left.length() if not is_zero_approx(move_speed): move_angle = atan2(transformed_left.x, transformed_left.y) on_update.emit(move_speed * delta, move_angle, y_angle, other_value)
or share this direct link: