I am making a game that is primarily going to be played with a controller, and I've ran into a few things I haven't been able to figure out on my own or in the documentation. First, is there a way to detect how far you are pushing the joystick? And if so, how do you use it? And second, how do you detect the direction it is being pushed in?
Controller Input Questions
The Controllers, gamepads and joysticks documentation page should contain everything you need :)
Alright I'll check it again.
It was helpful for one of the things, but I found nothing about how to see what direction you're pointing the joystick in.
- Edited
@Experiencepoints said: It was helpful for one of the things, but I found nothing about how to see what direction you're pointing the joystick in.
This part should do it:
var velocity = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
velocity
is a Vector2 that will contain the joystick direction, assuming the move_left
, move_right
, move_forward
and move_back
actions are all bound to the same stick (and on the correct axes).
Yeah, I saw that, but I am not sure how to set a sprite's direction to the Vector2. I tried assigning the Vector2 to a Sprite.direction and it didn't work.
Do you mean the rotation? If so, you can convert a Vector2 to a rotation using atan2
or the angle
function:
var velocity = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
$Sprite.global_rotation = velocity.angle()
# Or
$Sprite.global_rotation = atan2(velocity.y, velocity.x)
Ok thanks! I have no idea why I didn't think of using atan2, I used to use it all the time when I made games with processing.js.
Oh, that function looks pretty handy.