0 votes

Hey!

It's me again... So I'm making a 2d Platformer game and when I want to do moveandslide
I get this error "Invalid type in function 'moveandslide' in base 'KinematicBody2d'. cannot convert argument 1 int to Vector2"

my moveandslide 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! :)

in Engine by (22 points)

1 Answer

+1 vote
Best answer

Here is the error:

   else:
        velocity = 0
by (2,260 points)
selected by

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.