1
\$\begingroup\$

I'm beginning with Godot making a simple Sprite which should move both left and right but when right key is pressed the KinematicBody2D doesn't move right. According to the logic it should does but it does not.

I leave the code below hoping someone could help me to find the error

extends KinematicBody2D

const VEL = 50
var vec_mov = Vector2()

func _ready():
    pass # Replace with function body.

func _physics_process(delta):
   if(Input.is_action_pressed("ui_right")):
       vec_mov.x += VEL
   if(Input.is_action_pressed("ui_left")):
       vec_mov.x -= VEL
   else: 
       vec_mov.x = 0

   move_and_slide(vec_mov)

Thanks

\$\endgroup\$
2
\$\begingroup\$

Your problem is in the code here:

   if(...):
       A
   if(...):
       B
   else: 
       C

You are causing it to go into C only if it does not go into B. The statement about A is a separate statement and has no effect. So the velocity value is getting turned off because you are not in B.

Try putting all the statements together like this:

   if(...):
       A
   elif(...):
       B
   else: 
       C

It should fix your problem.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you so much. Now its moving both sides and looks cool! :D \$\endgroup\$ – EmmanCanVaz_95 Oct 27 '19 at 2:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.