Flip character to the left/right based on movement

const SPEED = 300.0
const JUMP_VELOCITY = -400.0
@onready var sprite_2d = $Sprite2D
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta):
	if not is_on_floor():
		velocity.y += gravity * delta
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY
	var direction = Input.get_axis("ui_left", "ui_right")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()
	
var isLeft = velocity.x < 0
sprite_2d.flip_h = isLeft

Question

I’m trying to insert the last two lines, but it gives me the following error:
Line 20:Unexpected “Identifier” in class body.

The problem itself presents when I insert the var ‘sprite_2d’

Obs: this is my first time experiencing codes, so i’m quite the newbie =(
I’m following this video to learn:

I can’t really tell because you didn’t format it correctly (you can do it by surrounding it with three backticks) but I think you didn’t indent the line so that’s inside the function? At least, that’s a common reason for the error.

I’ve updated it, please take a look now

The last two lines need to be indented so that they are inside the function

1 Like

Ohhh, this was the solution.
Thank you!!!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.