How to work with collision in godot

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

Hi, I’ve veen trying to add collision to my player and the environment, but adding collision in godot is way harder than i thought, i thought all we had to do was add the collision node to each of the object, add the collision box, then collision would work, but only thing that happens is that the player goes through the object so i tried adding it with scripts in many ways such as:

#player script
if(!get_node(".").is_colliding()):
   set_pos(get_pos() + Vector2(0, -16))
   anim.play("movement")
   anim.seek(0, true)
   yield(animator, "finished")
   emit_signal("move")
   pass

and this:

#player script
 if(!get_node(".").is_colliding()):
     if(Input.is_action_pressed("ui_up"):
         move up()

I even tried this:

#player script
  function _ready():
     add_to_group("colliders")

  func collision_check(body):
     if(body.is_in_group("colliders")):
          set_pos(0)   

#other object script
 func _ready():
    add_to_group("colliders")

None worked, I really need help working with collision in godot

Could be simple or complicated depending on your design, care to share the scene structure? just that piece of code says too little about your problem.

eons | 2017-03-11 10:20

Not really sure how to, but for the player, the scene system is:

kinematicbody2D (with script)
- animatedsprite
- collisionshape2D
- animation

as for the object:
staticbody2d (with script)
- animation

hope this helps

Dava | 2017-03-11 14:34

:bust_in_silhouette: Reply From: eons

KinematicBody2D (and KinematicBody) is_colliding will return true only if collides after using move.

move and move_to are methods to use if you want an overlap-prevention movement on kinematic bodies, otherwise (changing position, transform) they will move like static bodies, overlapping other bodies and pushing rigid bodies (like in any other physics engine).

There are 2 demos with 2D kinematic characters and one 3D that you can look at, also the official docs with detailed explanation on kinematic characters http://docs.godotengine.org/en/stable/tutorials/2d/kinematic_character_2d.html

Thanks it worked :smiley:

Dava | 2017-03-11 17:46