Keep getting move_and_slide error...

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

Hey!

It’s me again… So I’m making a 2d Platformer game and when I want to do move_and_slide
I get this error “Invalid type in function ‘move_and_slide’ in base ‘KinematicBody2d’. cannot convert argument 1 int to Vector2”

my move_and_slide code is velocity = move_and_slide(velocity, Vector2.UP)
and I think this error means that my velocity is a integer but my velocity is Vector2(0, 0)

Here is the full code: `
extends KinematicBody2D

var speed = 100
var gravity = 100
var jump_force = 100
var velocity = Vector2(0,0)


func _physics_process(delta: float) -> void:
velocity.y += gravity

if Input.is_action_pressed("ui_left"):
	velocity.x = -speed
elif Input.is_action_pressed("ui_right"):
	velocity.x = speed
else:
	velocity = 0

if Input.is_action_just_pressed("ui_up") and is_on_floor():
	velocity.y = -jump_force

velocity = move_and_slide(velocity, Vector2.UP)

If you can help me then please do.
Thanks in advance! :slight_smile:

:bust_in_silhouette: Reply From: estebanmolca

Here is the error:

   else:
        velocity = 0

I leave the clarification: when that line is executed, velocity is 0 and 0 is an int.
It should be velocity.x = 0

estebanmolca | 2021-08-21 12:36