0 votes
extends CollisionShape2D

var score : int = 0

var speed : int = 200
var jumpForce : int = 600
var gravity : int = 800

var vel : Vector2 = Vector2()

onready var sprite : Sprite = get_node("Sprite")

func physicsprocess(delta):

vel.x = 0

if Input.is_action_pressed("move_left"):
    vel.x -= speed
if Input.is_action_pressed("move_right"):
    vel.x += speed

move_and_slide

Im trying to use the move_and_slide function in my Kinematic2D script but it raises an error. Please help me clarify this doubt as I am new to Godot thanks.

Godot version version 3.5.1
in Engine by (12 points)

1 Answer

+1 vote

Double check which node the script is attached. In the beginning, extends CollisionShape2D tells that it isn't in KinematicBody2D, but it's collision shape. Try detaching the script from that node and attach it to the body (and change the first line correspondingly).

Also, I'm not sure if you omitted that part intentionally, but I'll say it anyway: move_and_slide needs speed as a parameter: move_and_slide(vel)

by (1,100 points)
edited by

Thank you so much, it works fine now.

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.