Godot Version
godot 4.3
Question
i have joystick, but its moves separated from look direction, i mean if joystick go up, char not go forward, it go up, how i can fix to char moves not global axises, but regardly his look direction?
godot 4.3
i have joystick, but its moves separated from look direction, i mean if joystick go up, char not go forward, it go up, how i can fix to char moves not global axises, but regardly his look direction?
generally you would apply the same rotation your camera has to the input’s value.
var input_dir := Input.get_vector("left", "right", "up", "down")
var direction := input_dir.rotated(rotation)
its for 2d for joystick?
“2d joystick” sounds like a lot of things, do you mean a game controller? Maybe it would be easier to understand if you posted some code or a screenshot.
Generally though if you are rotating the camera, you need to also rotate the input as I’ve posted.
extends CharacterBody2D
@export var speed = 1000
@export var sense = 0.010
func get_input():
velocity = ((transform.x * Input.get_axis(“back”, “forward”)) + (transform.y * Input.get_axis(“left”, “right”))) * speed
func _physics_process(_delta):
#get_input()
move_and_slide()
func _input(event: InputEvent):
if event is InputEventScreenDrag:
rotate(event.screen_relative.x * sense)
#if event is InputEventMouse:
#rotate(event.screen_relative.x * sense)
func _on_joystick_jspressed_sig(pos: Vector2) → void:
velocity = pos*speed
extends Sprite2D
@export var knop_clampzone = 150
@export var deadzone = 25
var is_pressed : bool
var touch_idx = -1
@onready var base = $JSring
@onready var tip = $JSknob
signal JSpressedSig(pos:Vector2)
func _ready() → void:
pass
func _process(_delta: float) → void:
if tip.position != Vector2.ZERO and base.position.distance_to(tip.position) >= deadzone:
emit_signal(“JSpressedSig”,((tip.position - base.position)/knop_clampzone))
if base.position.distance_to(tip.position) < deadzone:
emit_signal(“JSpressedSig”,(Vector2.ZERO))
func _input(event):
if event is InputEventScreenTouch:
if event.pressed:
if base.get_rect().has_point(to_local(event.position)):
touch_idx = event.index
is_pressed = true
else:
touch_idx = -1
is_pressed = false
tip.position = Vector2.ZERO
emit_signal(“JSpressedSig”,(Vector2.ZERO))
if event is InputEventScreenDrag:
if is_pressed and event.index == touch_idx:
move_joystick()
func move_joystick():
var pos = get_global_mouse_position()-position
pos = pos.limit_length(knop_clampzone)
tip.position = pos
Make sure to paste with proper formatting
Look like you did account for some rotation before in this function:
You could rotate the input in this signal-connected function, using transform though:
func _on_joystick_jspressed_sig(pos: Vector2) -> void:
velocity = pos.rotated(rotation) * speed
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.