Unexpected Token: if:

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

So I just started learning how to code and decided to use a tutorial to help me get started, but even though I copied the code almost 1-1 with few exceptions that would only be cosmetic changes not actual changes to the code, I’m getting an error when trying to use “if”

this is my code

extends KinematicBody2D

var curHp: int = 40
var maxHp: int = 60
var moveSpeed : int = 250
var damage : int = 10

var cash : int = 30

var curLevel : int = 0
var curXP : int = 0
var xpToNextLevel : int = 50
var xpToLevelIncreaseRate : float = 1.2

var interactDist : int = 70

var vel : Vector2 = Vector2()
var facingDir : Vector2 = Vector2 ()

onready var rayCast = get_node(“RayCast2D”)

func _physics_process (delta):

vel = Vector2()

# inputs

if Input. is_action_pressed(“move_up”):
facingDir = Vector2(0, -1)
if Input. is_action_pressed(“move_down”):
facingDir = Vector2(0, 1)
if Input. is_action_pressed(“move_left”):
facingDir = Vector2(-1, 0)
if Input. is_action_pressed(“move_right”):
facingDir = Vector2(1, 0)

vel = vel.normalized()

move the player

move_and_slide(vel * moveSpeed)

:bust_in_silhouette: Reply From: kidscancode

The first problem is that your code is impossible to read because when you pasted it in here, it lost all formatting. There is a “Code Sample” button when you’re posting - use it to preserve your code’s formatting.

At a guess, I’d say your error is because the if is not indented correctly, so it’s not part of the _physics_process() function like it should be. vel = Vector2() is indented correctly, which is why it doesn’t trigger the error.