godot always runs the last if statement

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

im new to godot just started learning , i tried to make a movement for the player but the scrips always runs the last if statlment ignoring the rest before

this is the scritp i had

extends KinematicBody2D

export(int) var speed = 100

var movement_direction := Vector2.ZERO

func _process(delta):

if Input.is_action_pressed("right"):
	movement_direction.x=1
if Input.is_action_pressed("left"):
	movement_direction.x=-1



	movement_direction = movement_direction.normalized()
	move_and_slide(movement_direction*speed)

In future, when pasting code here, use the “Code Sample” button to format it correctly.

kidscancode | 2021-09-04 00:15

:bust_in_silhouette: Reply From: kidscancode

Because of the way you’ve pasted your code it’s hard to tell if your formatting is correct. However, it does look like those last two lines are indented so that they are under the second if statement. That is why pressing “right” won’t do anything.

Also, if you’re moving a KinematicBody2D with move_and_slide() you should be using _physics_process() rather than _process().