How make The player move?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By GameLike

Hello i am New în programation world i try to make a game and The make The player move
But idk why is not working i try to follow tutorial on yt but is
Say me the same error:" The argument ‘delta’ is never used in The function ‘_physcs proces’
If somebody ca-n help me or say me the The corect Code i apreciate

   extends Kinematicbody2D
  
   var motion = Vector2()

   func _physics_process(delta) :
         
           If Input.is_action_pressed("ui_right")
                 motion.x += 100
           elif Input.is_action_pressed("ui_left")
                 motion.x - = 100
           else:
                   motion.x = 0
           If Input.is_action_pressed("ui_up")
                 motion.y - = 100
           If Input.is_action_pressed("ui_down")
                 motion.y += 100
            else:
                    motion.y = 0
             
             pass
:bust_in_silhouette: Reply From: MCCV

First thing: the error you are getting is not and error but a warning, this message just means that the variable “delta” on your fifth line is never used.

The reason why it is not working is because you are not doing anything with the motion vector. You need a movement function for a kinematic body to move.

let me explain:
In the beginning of the script you define a variable called motion with the type Vector2

If you know what a vector is skip this paragraph. A vector2 is a 2 dimensional vector. A vector is a arrow (line segment) with a direction and magnitude, it represents this by 2 values a x and a y these represent the end point of the arrow relative to it’s origin. A vector does not have a position only a direction and magnitude (in most engines you do use vectors for position). What you do with this vector is up to you.

In this case you want the engine to move the player from the origin of the vector to it’s end we do this with the either the function move_and_slide() or move_and_collide(). We will use the move_and_slide() function. This function moves the object’s position variable from it’s original position (origin of our motion vector) to the position it has to go (the vector’s end point), when another physics object is in the way it wil collide and slide against it.

so it will look like this:

   extends Kinematicbody2D

  var motion = Vector2()

  func _physics_process(delta) :

       If Input.is_action_pressed("ui_right")
             motion.x += 100
       elif Input.is_action_pressed("ui_left")
             motion.x - = 100
       else:
               motion.x = 0
       If Input.is_action_pressed("ui_up")
             motion.y - = 100
       If Input.is_action_pressed("ui_down")
             motion.y += 100
       else:
                motion.y = 0

       motion = move_and_slide(motion * delta)

The delta is the frame time (the time between each frame). we do this to make the motion smooth and at the same speed regardless of FPS. in short if your position is ( 0, 0) and your motion is (1,2) your character will move 1 down and two to the right so after a second (due to the delta we used) the position will be (1,2) and after a second again it will be (2,4) etc…

I wish you luck on your project, and welcome to world of game development!