Joystick based rotation

kwakwarakwakwakwarakwa Posts: 1Member

Hi,
I have some issues with joystick-based rotation of sprite. In 2d topdown game I want to rotate turret using right gamepad stick. Turret should rotate in direction of stick, in every direction. The problem is, that because analog axes can have values -1 to 1, and rotation is based on Vector2 of axes, it turns smoothly only in 8 directions. Aiming in other angles (other than 0, 45, 90 etc) is possible but hard. Is this because of poor joystick support? Or I'm using wrong methods?

if abs(Input.get_joy_axis(0, JOY_AXIS_3)) > deadZone or abs(Input.get_joy_axis(0, JOY_AXIS_2)) > deadZone:
        cannon_stick_dir = Vector2(Input.get_joy_axis(0, JOY_AXIS_3) * -1, Input.get_joy_axis(0, JOY_AXIS_2))
    var cannon_position = $Cannon.get_rotation()  * 0.85 
    if cannon_position != cannon_stick_dir.angle():
        cannon_dir_vector = Vector2(cos($Cannon.get_rotation()), sin($Cannon.get_rotation())) * -1
        if cannon_position != cannon_stick_dir.angle():
            if cannon_dir_vector.angle_to(cannon_stick_dir) >= 0:
                rot_dir -= 1
                $Cannon.rotation = (rot_speed * rot_dir * delta) 
            elif cannon_dir_vector.angle_to(cannon_stick_dir) < 0:
                rot_dir += 1
                $Cannon.rotation = (rot_speed * rot_dir * delta) 
            else:
                rot_speed = 0
Tagged:

Tags :
Sign In or Register to comment.