The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+1 vote

I'm trying to make some code that once the player has collided with another KinematicBody2D, which has a Sprite and a CollisionShape2D as its child, It will make itself hidden or delete itself. Am I on the right track with this code?

extends KinematicBody2D

func _ready():
    set_fixed_process(true)

func _fixed_process(delta):
    if is_colliding():
        self.hide()
in Engine by (164 points)

1 Answer

+1 vote

First, only a moving kinematic body (with move) will collide, the non moving will never detect because kinematics normally never detect collisions.

So, in the player code you may have:

func _fixed_process(delta):
  some_code
  more_code

  move(motion)

  if is_colliding():   #only collides after move
     var collider = get_collider()

     if collider.is_in_group("bodies_that_die_when_player_touch_them"):
        collider.queue_free() 

Later you may want some processing before deleting instead of removing it from the player code (is better to let an instance to take care of its own death), like remove collisions, play sounds, change sprites, animations, fire particles and make that node queue_free itself after finishing all that fancy dying process.

Every body in the group that can be killed by the player should have a common function to call and do all that (like die, hurt, etc.).

by (7,890 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.