Using move_and_collide() stops when against a static object

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dumuz

I’ve tried doing what the docs say:

For example, the following two code snippets result in the same
collision response:

# using move_and_collide
var collision = move_and_collide(velocity * delta)
if collision:
    velocity = velocity.slide(collision.normal)

# using move_and_slide
velocity = move_and_slide(velocity)

But this NOT the case, I have practically copied this script verbatim and the 'Player is still stopped on staticbody2D. It is some how STILL colliding and just choosing to stop instead of sliding across if approached at a diagonal vector. If only goes across a staticbody2D if I only press a perpendicular direction to it.

Here is MY script:

const speed = 500
var velocity = Vector2()

func get_input():
	velocity = Vector2()
	if Input.is_action_pressed("ui_right"):
		velocity.x += 1
	if Input.is_action_pressed("ui_left"):
		velocity.x -= 1
	if Input.is_action_pressed("ui_down"):
		velocity.y += 1
	if Input.is_action_pressed("ui_up"):
		velocity.y -= 1
	if velocity.length() > 0:
		velocity =  velocity.normalized() * speed
	#print(velocity)

func _physics_process(delta):
	get_input()
	var pcol = player.move_and_collide(velocity * delta)
	if pcol:
		velocity = velocity.slide(pcol.normal)
	print(velocity)

I’ve tried taking the velocity and stepifying the axis that is being nullified by the slide(), in order to make sure it’s zeroed out, but it doesn’t matter, it still colliding. I believe it’s because it’s calling the var pcol first before its collision, so it just rapidly fires a stop.

Is there a way to use move_and_collide without it stopping at very staticbody2D? I want it to just slide along it.

:bust_in_silhouette: Reply From: theMX89

use this: move_and_slide()

with underscores: move_and_slide

theMX89 | 2021-06-12 07:08

Thanks, My aim was more trying to figure out how to make move_and_collide do the same thing as move_and_slide, since move_and_collide is a derivative of move_and_slide.

But thank you!

Dumuz | 2021-06-12 22:30

:bust_in_silhouette: Reply From: Dumuz

This post answered my question:

move_and_slide use move_and_collide internally. It just calls
move_and_collide again once collision is occurred

So now the script looks like this:

func _physics_process(delta):
	get_input()
	var pcol = player.move_and_collide(velocity * delta)
	if pcol:
	    velocity = player.move_and_collide(velocity.slide(pcol.normal) * delta)