So someone pointed out to me that the Input.get_vector()
function limits the length of the vector to 1, so there is no reason to normalize it. Which made me wonder why all the tutorials I saw normalized the vector. So I went and looked up some top-down movement tutorials on YouTube, and the reason people were calling Vector2.normalized()
is because they were not using the Input.get_vector()
function at all!
They were using Input.get_axis()
two times for the x and y axes separately (which isn’t necessary for a 2D top-down game) or using Input.get_action_strength()
four times for each direction, or even Input.is_action_pressed()
. Using this method means that they have to normalize the input because the method won’t limit the length for them.
I’m not sure why this is. Input.get_vector()
was added in 3.4, which was released in Nov. 2021 (all the tutorials I looked at were for Godot 4). Before that, the tutorial on the docs did call Vector2.normalized()
, so maybe people are getting their knowledge from that tutorial, or maybe other game engines where you have to use a normalizing function as well?
Something to take into consideration when normalizing the input vector is that while it won’t change anything for keyboard movement, joystick movement will be affected. The action strength of a key press is either 1.0 or 0.0, but a joystick can be any number in between. Normalizing the input vector from a joystick will result in the length being 1.0 or 0.0, so the player can’t halfway move the joystick to walk slower.
The tutorials I watched are for beginners, so they probably aren’t going to be connecting their XBox controller or whatever and trying to move their character with the joystick, but I think it would be better to put simpler code in the tutorial that also won’t break with different forms of input.
Basically, this code:
var input := Vector2(
Input.get_action_strength("right") - Input.get_action_strength("left"),
Input.get_action_strength("down") - Input.get_action_strength("up")
).limit_length(1.0)
can be shortened into this code:
var input := Input.get_vector("left", "right", "up", "down")