Was following this tutorial https://www.youtube.com/watch?v=QoNukqpolS8 and decided to use only two directions instead of the 4 he used because of what my specific game needed. however, i encountered the error 'Error with GDScript ‘No constructor of “Vector2” matches the signature "Vector2(float)’. i tried to fix the problem by adding ‘dummy’ controls (controls with no keybinds) and while that fixed the script issue, now the player won’t move. can anyone help with this or provide a similar bit of code i can use so that only 2 controls are possible? would be a big help as this is for a school assignment i am behind on, big time.
(comment i wrote about this on the video LINK REPLACED WITH ERROR 404. IDK WHY IT JUST IS)
Basically, Vector2 has 2 components, x and y. If you want to lock movement to only 2 directions (e.g. up and down, or left and right) you still need to pass both x AND y, just make sure one of them changes and the other remains the same.
can i please ask how exactly i would code that? would i assign x and y to left and right or would i keep the dummy directions and assign x and y to them?
Ok, at a glance the reason the video is a little hard to understand is probably just because the whole thing is done in one line, so you may not be noticing how the brackets work.
Breaking it down:
var direction = Vector2(Input.get_axis("move_left", "move_right"), Input.get_axis("move_up", "move_down"))
can be thought of like this:
var direction = Vector2(
Input.get_axis("move_left", "move_right"), # The x component of the Vector2 is the value returned from the 'get_axis' function of the Input class.
Input.get_axis("move_up", "move_down") # The y component of the Vector2 is the value returned from the 'get_axis' function of the Input class.
)
so what does Input.get_axis() do?
So, pseudcoding the above:
Direction = Vector2(
left and right movement input ranging from -strength to strength (usually 1.0)
up and down movement input ranging from -strength to strength (usually 1.0)
)
So finally, to ignore the input for a particular axis, i.e you only want left and right movement, you still need to pass the x AND y component to Vector2, but one of them can be set statically to 0:
var direction = Vector2(Input.get_axis("move_left", "move_right"), 0) # note the 0 for the Y never changes
As a final note, if a video claims to teach you how to make a whole game in a hour 30, it is probably going to be making some pretty quick jumps between concepts. A good place to start for a beginner game dev, but not so much a beginner coder.