Collisions with instanced objects?

ImerionImerion Posts: 7Member
edited January 2017 in 2D

Now I have tried to solve this collision problem for the entire day without success. I have an object called "Bullet" that is a KinematicBody2D with a CollisionShape2D. It is instanced from another object called "Unit1". In my main scene I spawn a few Unit1 and those in turn spawn (instance) Bullets. Bullets register collisions fine with another KinematicBody object that is part of my main scene. However, Bullets DO NOT register collisions with Unit1. Unit1 (that instances bullets) is also a KinematicBody2D with a CollisionShape2D. I can't imagine what is wrong.

This code, in the Bullet-scene, works fine:

    func _fixed_process(delta):
        if (self.is_colliding()):
            if (self.get_collider().get_name() == "Player1Shield"):
                Globals.PlayerOneHealth -= 1
            if (self.get_collider().get_name() == "Player2Shield"):
                Globals.PlayerTwoHealth -= 1

This code, in Unit1, does nothing:

func _fixed_process(delta):
    if (self.is_colliding()):

Is it not possible to make instanced objects check for collision with the object that instanced them?

Thankful for any help! :D


Tags :


  • TwistedTwiglegTwistedTwigleg Posts: 453Moderator

    I think that kinetic bodies can collide with the object that instances them...

    Maybe your bullet code is destroying the bullet before the Unit1 notices it got hit?
    Have you tried commenting out the "self.queue_free()" code in the bullet scene? Does it work then?

    Also, your code in Unit1 wouldn't have any visible effect (unless there is code below it). I would add a print statement to it, so that is something like this:

    func _fixed_process(delta):
        if (self.is_colliding()):
            print (self.get_collider().get_name())

    Then you can see in the console if it worked or not.

    Another thing you could try, is make the bullets a child of Unit1's parent. Maybe that will work?

  • ImerionImerion Posts: 7Member

    Hi! Thanks for your answer!

    Originally the code did say print first, but that somehow got lost. :)

    It's still a bit odd though. I can get collisions on the bullet-side fine, but not on the unit side. Perhaps the bullets get "stopped" before they have time to trigger a collision on Unit1? How would I go about to disable the "stop"-part of the collision and only make it get colliders while continuing to move?

  • TwistedTwiglegTwistedTwigleg Posts: 453Moderator
    Answer ✓

    Out of curiosity, are you trying to tell when bullet's collide with other unit1s? Or the same Unit1 that spawned it? Not that it really matters, I'm just curious.

    It is possible that the bullets are "stopped" before they have time to trigger a collision for the unit1 it collides with. If you are destroying the bullets on impact, maybe add a small timer before destroying them? If not, maybe freeze the bullets location for a second and see what happens?

    A workaround you could try is calling a function from the bullet when it collides with something. Your unit1s could have a function that does all of the processing it needs to do, and have the bullet(s) call it when they collide with unit1s. It would look something like this (for your bullets):

    NOTE: you would have to have a variable called name in your unit1 script for this example to work!
    You could use any variable though, I just chose name for an example.
    if (self.is_colliding()):
        var other = self.get_collider()
        if ( == "unit1"):

    Then you would just have to have a collision function in your unit1s.

    you could also use an Area2D instead of a kinetic body, and move the object with translate instead of move. Then you could use the collision signals for checking. Not sure how useful it would be, nor if the issue would persist, but I'm just throwing ideas around :)

    I agree it's rather strange, maybe you found a bug?

  • ImerionImerion Posts: 7Member
    edited January 2017

    It's supposed to collide with any other unit1.

    I doesn't seem to have anything to do with them getting destroyed or stopped. Calling a function like that worked however! I'm still not getting any readings on the unit1-side, but at least this makes everything work. Maybe it is a bug.

    Thanks either way!

  • TwistedTwiglegTwistedTwigleg Posts: 453Moderator

    Glad you got it working!

Sign In or Register to comment.