0

"the identifier "velocity" isn't declared in the current scope."

godot is showing this error in my code:

extends KinematicBody2D

func _physics_process(delta):
       if Input.is_action_pressed("ui_right"):
          velocity.x += 4 
       elif Input.is_action_pressed("ui_left"):
          velocity.x += -4

for some reason, I can't find the reason why this happens pls answer

maybe the problem is with my code as I am purely a beginner and i am using godot version-3.4.2

  • Godot claims `velocity` is not declared. You claim Godot is wrong on that regard. Well, where did you declare `velocity`? - I suspect you want to handle the `KinematicBody2D` as a `RigidBody2D` (which has a property `linear_velocity`). Are trying to translate code from another engine? I suggest to start with a Godot dedicated tutorial. – Theraot Feb 04 '22 at 10:07
  • @Theraot thanks you for your response, i actually saw this code from a youtube tutorial and i just started with godot so this code ```if Input.is_action_pressed("ui_right"): velocity.x += 4 ``` was meant to move the ```kinematicbody2d``` when right key is pressed and godot is my first engine soo –  Feb 04 '22 at 10:15
  • 1
    You move the `KinematicBody2D` by calling `move_and_slide`, which takes a velocity as fist parameter. I would expect `velocity` to be declared as a variable, and then passed to `move_and_slide`. Something like this: https://docs.godotengine.org/en/stable/tutorials/2d/2d_movement.html#doc-2d-movement - I suppose they do something similar in the video. – Theraot Feb 04 '22 at 10:21
  • thanks you so much for your help edit: yes, it solves the problem –  Feb 04 '22 at 10:24

0 Answers0