Unexpected Token: "if"

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

Here is my code:

var curHP : int = 10
var maxHP : int = 10
var damage : int = 1

var gold : int = 0

var attackRate : float = 0.3
var lastAttackTime : int = 0

var moveSpeed : float = 5.0
var jumpForce : float = 10.0
var gravity : float = 15.0

var vel = Vector3()

onready var camera = get_node("CameraOrbit")
onready var attackCast = get_node("AttackRayCast")

func _physics_process (delta):
	vel.x = 0
	vel.z = 0

var input = Vector3()


if Input.is_action_pressed("move_forwards"): <--- Error
    input.z += 1
if Input.is_action_pressed("move_backwards"):
    input.z -= 1
if Input.is_action_pressed("move_left"):
    input.x += 1
if Input.is_action_pressed("move_right"):
    input.x -= 1

input = input.normalized()

vel.x = dir.x * moveSpeed
vel.y = dir.y * moveSpeed

vel.y -= gravity * delta

It says “unexpected token: “if””

:bust_in_silhouette: Reply From: kidscancode

Note: I edited your post and clicked the “Code Sample” button to properly format your pasted code. Please make sure to do this in the future as it makes the code easier to read.

All the code after vel.z = 0 needs to be inside the _physics_process() function, which means it all needs to be indented. Code can’t be executed outside of a function, which is why you get an “unexpected if”

Thanks, I only started using Godot about a week ago.

Immortal Arts | 2021-08-30 21:08