0 votes

I am trying to understand the basic concepts of KinematicBody2D. Following is my code for movement of a sprite.

extends KinematicBody2D
const acceleration=50
const MAX_SPEED =200
var velocity = Vector2.ZERO

func ready():
set
physics_process(true)

func physicsprocess(delta):

velocity.x += (acceleration * delta)
velocity.x = clamp(velocity.x,-MAX_SPEED,MAX_SPEED)
move_and_collide(velocity)

I don't understand that when I am using move and collide method for movement, the sprite quickly moves out of the screen. But when using move and slide method(without multiplying acceleration with delta),the movement is slow and looks perfect.
I have also noted that when the velocity is set constant, the sprite movement is same using both method.

in Engine by (16 points)

2 Answers

0 votes

Hello,

I think you should be multiplying acceleration by delta, and adding it to velocity in both cases.

The difference is whether you then manually multiply the velocity vector by delta when passing it to the movement function.

For moveandcollide, you do moveandcollide(velocity * delta). For the moveandslide helper function, you do moveandslide(velocity) as the helper function multiples by delta internally.

by (394 points)
0 votes

When you use move_and_collide your speed is 200px per frame
With move_and_slidethe speed is 200px per second, this methods also returns a Vector2D which represents your remaining velocity.

by (178 points)
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.