Why can I push a KinematicBody2D with move_and_collide?

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

Player and NPC walk like this:

func _physics_process(delta):
     # calculate velocity
     move_and_collide(velocity * delta)

Now, let’s say an NPC is walking to the right and the player runs into him from the top. I want the player to stop immidiately and the NPC to move along. But in reality, the player can go downwards at a very low speed, pushing the NPC. The NPC can do the same to the player as well. Maybe the same reason I can “slide” on the floor slightly if I hold down and right at the same time?
What’s happening here? And what can do to fix this? Any help would be appreciated.

:bust_in_silhouette: Reply From: klaas

the docs state …

A kinematic character controller is assumed to always begin in a non-colliding state, and will always move to a non-colliding state. If it starts in a colliding state, it will try to free itself like rigid bodies do, but this is the exception, not the rule. This makes their control and motion a lot more predictable and easier to program. However, as a downside, they can’t directly interact with other physics objects, unless done by hand in code.

the statement in bold is the reason for your sliding movement.

I would try to reset the kinematic body to the position before the collision has happend. This will stop the object from sliding along.

Can I ask how you would reset the kinematic body’s position? I put this in both character’s script:

var collision = move_and_collide(velocity)
if collision:

It did stop the sliding but caused many other problems: now I’m pushed back when I try to push him head-on, and characters vibrate when trying to push things. Am I doing it wrong?

Haseb | 2020-09-03 19:25

those are many more questions.

I dont understand your setup … some things should be pushed others not. So you have to branch your collision reaction by the sort of the collider.

klaas | 2020-09-03 19:44

Sounds too complicated for me… I’m going to just stop npcs when they are close to the player for now. Or maybe let npcs pass through the player. Anyway, thank you for your help :slight_smile:

Haseb | 2020-09-03 20:12

hey … just hang in there. It aint easy yet but will be soon. :slight_smile:
Maybe check some example projects for there method of dealing with those things.

klaas | 2020-09-03 20:16