Hi!
I have an issue with enemy type i try to add to learning project I make.
first of all, this is my second game so things are still very confusing sometimes and I can't seem to resolve this issue for weeks now...
What I want:
turret type enemy, that will be spawned in game level (instantiate). > look at player (plane) > shoot on timer >repeat
I made it but it works fine if I instantiate (add) this enemy to game level in editor.
set up that works looks like below:
turret code:

turret enemy nodes:

on signal in game level code:

but If I want to instantiate this through and had to make code just inside enemy then I have problems.

new code as below:
new turret code

node set up

additional line of code in node inside of turret just to turn turret with body

in game look with both turrets which works similar but.

both look at player > shoot on timer
new spawns bullets from center (where projectiles node is made)
old spawns from marker which is supposed to be spawning place
What's the main issue? well the new one hits it's own collision body and self destroys because bullets are spawned inside of it.

what change? 😃

  • Toxe replied to this.

    An_unique_name Did you change the collision layer & mask flags of your Area2D colliders so that your bullets don't collide with the turrets?

      Toxe yes, but I made it so bullets can collide with turrets.
      I know this workaround and will change that enemies can't hit other enemies neither themselves, and plane can't hit plane.
      however, it doesn't solve the issue, it just does not look good when bullets spawn from middle of the turret instead of end of a barrel

      • xyz replied to this.

        xyz I am kind of doing this with marker 2d.
        I'm taking it's position and spawn on it
        "bullet.position = turret_marker.global_position.normalized()"
        and it works in first example but not in second which I want to have.

        if I just spawn it on the marker it is in "body" nod of a turret and then bullets follows movement of barrel, that's why I have to instantiate it outside of node which looks_at player

        • xyz replied to this.

          An_unique_name Why do you normalize a position? Just spawn the bullet, parent it into global space and assign marker's global position to it.

            xyz this is auto-scroller type of a game, I spawn turrets on random position on x axis on top of the screen.
            after turret is spawned then it's spawning bullets on marker, but without normalized then distance from 0.0 point of turret node and main game level were added to spawning position of bullets so then turret was lets say on position 100,100 and bullets instead of spawning 110,110 was spawning 220,220.
            with normalized it works fine.... kind of... at least better 😃
            couldn't figure out better way to be honest

            and again I have it work fine in version one, but then I can't instantiate turrets randomly

            • xyz replied to this.

              An_unique_name Umm, you're needlessly complicating this. Spawn a bullet, add it as a child to main scene and just do bullet.global_position = marker.global_position. That's all there is to it. Normalizing positions is utterly meaningless. Normalization is for direction vectors.

                xyz but I have it this way. When you check my initial post you can see I made two versions of this turret, one I made like you suggest and it works fine. 3 first pictures explain this method
                But then I want to instantiate turrets as one of random enemies on random place on map this case I cannot fix signal from turret to main level to instantiate a bullet. That's where second version of the turrets come to play