move_and_slide() vs move()

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By edimemune
:warning: Old Version Published before Godot 3 was released.

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?

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.is_action_pressed()) - int(Input.is_action_pressed())
direction.y = int(Input.is_action_pressed()) - int(Input.is_action_pressed())
move_and_slide(direction.normalized() * speed)

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

batomow | 2018-06-14 04:40

:bust_in_silhouette: Reply From: eons

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:


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.