Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | jarlowrey |
Joypad inputs appear to only give you the direction, not a precise angle
I want the user to be able to aim precisely (setting their 2D angle to the joypad’s angle), but with the current input mapping I can only let them aim in 45 degree increments. Is there any way to allow 360 degree turning with joypad input mapping?
A partial solution to be used in _physics_process
. Unfortunately it ‘snaps’ the given aim_dir
angles.
func _aim(delta):
#KEYBOARD AIMING
var up = Input.is_action_pressed("aim_up")
var down = Input.is_action_pressed("aim_down")
var left = Input.is_action_pressed("aim_left")
var right = Input.is_action_pressed("aim_right")
if up or down or left or right: #do not change aim if none of those inputs were pressed
var aim_speed = 4
var aim_dir = 0
if up and right:
aim_dir = 315
elif up and left:
aim_dir = 225
elif down and right:
aim_dir = 45
elif down and left:
aim_dir = 135
elif down:
aim_dir = 90
elif up:
aim_dir = 270
elif left:
aim_dir = 180
elif right:
aim_dir = 0
aim_dir = deg2rad(aim_dir)
var diff = aim_dir - _body.global_rotation
if abs(diff) > PI:#rotate shortest direction
diff += - sign(diff) * 2 * PI
if abs(diff) < PI/50:#stop rotating when close
diff = 0
_body.global_rotation += sign(diff) * delta * aim_speed
jarlowrey | 2018-04-17 23:21