how to subtle push through a crowed of kinematic2D

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

I want to have the effect of shuffling through a crowed of enemies

Both player and hordes are kinematic2D, RigidBody is bit hard to control.
At the moment im using Area2D to detect if player are within push zone

if it is in the push zone, the logics are

1 - get velocity = enemy global Pos - Player global Pos
2 - move and slide (velocity * spd * delta)

this made the “push” seem unnatural, and sometimes the player just walk right through it

I don’t think i really get what you want to do, if a player is coming fast will it get really close to the pushing body before being pushed away, or is it more akin to the player sliding off an invisible barrier close to the hordes?

In the first case i guess you are in the right direction, but you will probably need to make the push force a function of the distance, something akin to the magnetic repulsion formula, where the closes you are the bigger the force. As to have some smoothness of how strong you are being pushed. (If you ever need to optimize this part of the code, just use a lookup table). I didn’t work much with areas, but you can use a RayCast when there is a collision within the Area to get the distance, that is if the Area doesn’t give it to you already.

For the second case, I believe you can achieve it by creating a larger circular collision box that hits only the player, by selecting the collision layers, and basically stops the player from getting closer than that distance, and since you are using move and slide you should get a sliding through effect, albeit i dont really think this is the way to go, but maybe worth a try.

tastyshrimp | 2020-01-13 19:06