First of all, you put var motion
in _physics_process()
so now you have a new local motion
variable that has no value. Delete this line. You've already declared the motion
variable as a class variable at the top.
Second, normalizing is a function performed on vectors. It means taking a vector and scaling its length to 1. So for example the vector (1, 1)
would become (0.707, 0.707)
. However, you can't normalize only the x component.
Finally, you're never assigning any value other than (0, 0)
to motion
so your body is never going to move in the move_and_slide()
call.
If you're new to working with KinematicBody2D, I recommend reading the following:Using KinematicBody2D
Also, for understanding vectors, I highly recommend reading this:
Vector Math