Invalid operands 'vector2' and 'float' in operator '+'

Godot Version

4.2

Question

I keep getting this error message when im trying to follow the tutorial on my first 2D game by the godot docs but whenever i press play it says “invalid operands ‘vector2’ and ‘float’ in operator ‘+’” here is the code i have been useing

extends Area2D

@export var Speed = 400 #How fast player will move (pixels/sec) the @export means that we can edit it in the inspector pannel.
var screen_size #Size of game window

Called when the node enters the scene tree for the first time.

func _ready():
screen_size = get_viewport_rect().size

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta):
var velocity = Vector2.ZERO # The players movement vector
if Input.is_action_pressed(“Right”):
velocity.x = +1
if Input.is_action_pressed(“Left”):
velocity.x = -1
if Input.is_action_pressed(“Up”):
velocity.y = +1
if Input.is_action_pressed(“Down”):
velocity = -1

position += velocity * delta
position = position.clamp(Vector2.ZERO, screen_size)

if velocity.length() > 0:
	velocity = velocity.normalized() * Speed
	$AnimatedSprite2D.play()
else:
	$AnimatedSprite2D.stop()

on which line exactly this occur?

should be velocity.y = -1

1 Like

thanks i dont know how i missed that.

1 Like

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