Strange collision behavior

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

Hi,

I have a KinematicBody2D “Player” and KinematicBody2D “Enemy”. The Player can move in all directions and controlled by a keyboard, and the Enemy can move from left to right and controlled by ‘AI’. Both are squares with CollisionShape2D attached properly.

Now, the strange behaviour is: when in _fixed_process in “Player” script I check if is_colliding(): it works well all the time except when the Player is colliding with the Enemy from left side of Enemy’s square or right side, if the Player is colliding from the top or bottom of the enemy square - it is triggering. The way how I “fixed” the issue is moved is_colliding(): check from the “Player” script to “Enemy” script and suddenly it started to work as expected - colliding is triggering all the time when the Player is colliding with the Enemy does not matter left, right, top of bottom of it…

While the issue was “fixed”, I am not sure why at the first place I had this issue? Just want to understand the reason behind of it… Maybe it is somehow related to the Enemy’s direction (left, or right only) but still don’t understand why moving is_colliding(): to Enemy script resolve the issue? (I think btw it is a good place where this logic actually should be but, just want to know why it did not work in Player script)

Here is the “Player” code https://github.com/paxer/marine_treasure_hunter/blob/master/scenes/main/player.gd#L49 from where I moved “is_colliding” to “Enemy” script for a reference https://github.com/paxer/marine_treasure_hunter/blob/master/scenes/enemies/enemy.gd#L31

:bust_in_silhouette: Reply From: eons

I’m not sure, but maybe, in the physics process, the server separates the player from the enemy, making the enemy not able to collide with the player.

What you should do is to separate the layers, if you don’t want the player to collide with the enemy, don’t put the enemy mask, and if the enemy should ignore items, add the mask where the player layer is and don’t add the items, each on his own layer, of course.

If items can be collected, maybe is better to make areas from them and let them react on the “body_enter”, by affecting the player or something else (like a score object), or, if items are too many to manage that way, add an area to the player that can be used to detect other areas and do the actions it need, filtering by groups or types.

thanks for advice about Area, will try this approach too!

Pavel Kotlyar | 2017-04-21 03:59

:bust_in_silhouette: Reply From: MrMonk
is_colliding() works only when the player/enemy is moving. So Player can also detect collision but only if it's moving at the time of collision 

ahh! thanks!

Pavel Kotlyar | 2017-04-21 05:31