I think I am doing something wrong but I can’t seem to find what it is. I am asking for help to figure out where I’ve gone astray.
According to the tutorial, with this script I should expect the following behavior:
the sprite2d node responds to directional input left and right (pressing my left and right arrow keys) by turning left and right when those inputs are pressed.
Actual behavior:
the sprite2d node does not respond to directional input and instead rotates continuously
If the code isn’t responding to key presses then it could be that the input map has not been set up.
if Input.is_action_pressed("ui_left"):
if Input.is_action_pressed("ui_right"):
if Input.is_action_pressed("ui_up"):
ui_left, ui_right, ui_up are input actions. They are found under Project/Project Settings/Input Map.
Click on Show Built In Actions and look at the list of input actions. If you don’t see those three actions then you must add them.
But they are usually there by default.
Are you pressing the arrow buttons? (You most likely are but I had to ask)
Click on the game window before trying the buttons. I noticed on 4.3 that I have to click on the game window as the editor keeps focus. (Not sure if that is a setting or what)
See Project Setting > Input Map and turn on Show Built-in Actions.
You can see “ui_left”, “ui_right” and “ui_up”, click on plus in right side of actions (for example “ui_left”) and select Keyboard Keys and OK.
Now in “ui_left” you have a Keyboard Key, click on edit (pencil in right side) and click on your keyboard left key and select OK.
Do it for 3 Actions (“ui_left”, “ui_right” and “ui_up”) and try.
What I tried:
I went into Project/Project Settings/Input Map.
I clicked on Show Built In Actions.
The “ui_left”, “ui_right”, and “ui_up” were set already, but I set them again for good measure.
I made sure I clicked on the game window before trying the buttons.
No change in behavior unfortunately. I noticed that since the sprite was spinning from the beginning - it looks like the “if” statement on line 11 is returning true at all times. The editor/engine believes that my right input is always pressed (it’s not). I have taken a screenshot to demonstrate:
According to this situation, maybe the problem is with your keyboard or the engine has a problem in receiving the keys, because this problem is not reasonable at all! But I have some ideas:
Restart your engine
Select other key: use a for left, d for right and w for up
Delete other events: only use Keyboard Keys and remove Joypad events