+2 votes

I have been learning Godot Engine for a month and I tryed to make a game from start to finish to gain experience. I made a ball with physics that moves with a tilemap. But I don't understand the difference between this two functions: move_and_slide() and move(). When I used move to move the ball, it was moving very slow when touching the ground, now when I am using move_and_slide() function it works properly.
Here is the code:

extends KinematicBody2D

export var viteza = 30
var animNod
var jumped = false
var falling = false

var y

func _ready():
    animNod = get_node("AnimatedSprite")
    set_fixed_process(true)

func _fixed_process(delta):

    #MISCARE

    delta *= 10
    var misc = Vector2(0,1)

    if(jumped):
        if(get_pos()[1] > y and not falling):
            print("Da")
            misc[1] = -1
        elif(get_pos()[1] <= y):
            falling = true

    if(Input.is_key_pressed(KEY_D) or Input.is_key_pressed(KEY_RIGHT)):
        misc[0] = 1

    if(Input.is_key_pressed(KEY_A) or Input.is_key_pressed(KEY_LEFT)):
        misc[0] = -1

    misc = misc * (delta * viteza)

    #here was move function
    self.move_and_slide(misc*100)

    #ANIMATIE

    var newAnim = "idle"

    if(Input.is_key_pressed(KEY_D) or Input.is_key_pressed(KEY_RIGHT) or Input.is_key_pressed(KEY_A) or Input.is_key_pressed(KEY_LEFT)):
        newAnim = "mers"

    #Jump --->

    if((Input.is_key_pressed(KEY_SPACE) or Input.is_key_pressed(KEY_W)) and not jumped):
        jumped = true
        var jH = 50
        y = get_pos()[1] - jH

    print(jumped)   
    #<--- Jump

    animNod.play(newAnim)

    var corp = get_collider()
    if corp != null:
        if corp.is_in_group("sol") and falling:
            jumped = false
            falling = false

What the difference between them? And what does each of them?

in Engine by (61 points)
edited by

Hello from the future!
Im using 3.0.2 stable and this glitch is still in place. Almost exactly the same. Im doing a:

var direction
direction.x = int(Input.isactionpressed()) - int(Input.isactionpressed())
direction.y = int(Input.isactionpressed()) - int(Input.isactionpressed())
moveandslide(direction.normalized() * speed)

When checking in the console, the sprite gets stuck when the direction is Vector2(0,1)

1 Answer

+2 votes
Best answer

I'm assuming you are using 2.1.4

All the "move" methods are part of an overlap-aware position setters API for the kinematic bodies (normally, kinematicbodies ignore everything).

move prevents overlaps, and that is all, what you experience when moving against another body is the effect of moving position>checking overlap>separating from collider.

If you want normal movement against solids, you will need to get the remainder of move and "slide" that vector along the collision surface.
Here is an example:
http://docs.godotengine.org/en/2.1/learning/features/physics/kinematic_character_2d.html


move_and_slide does all that for you, but you will have little control of the parameters used (and there is a bug in the wall/floor/ceiling checks, it will be fixed by 2.1.4.1).
This will be enough for most cases but sometimes you may prefer to have fine control of the results of a character motion.


Something else, move takes the full amount of movement you want to achieve, that is why you integrate velocity with the delta time when using it (to get the movement in that fraction of time), but move_and_slide does that internally so you need to use the whole velocity vector with it.

by (7,946 points)
selected by
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.