Help with turret behaviour

This forum is currently in read-only mode.
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • I'm wondering how to use the turret behaviour properly... at the moment I create a turret, then if the turret does not have a target, run "Add object to target" with the object in question a sprite in my game called "helicopter".

    Then I have an action that's triggered by the "on shoot" event on my turret, which spawns a bullet and sets the angle towards "helicopter".

    I hope that all made sense.

    Anyway, the turret rotates and fires as it should when helicopters pass in range, however, it seems it always shoots at the earliest helicopter. So a helicopter can fly in range, fly out of range, another helicopter will fly into range, and the turret will rotate to face the second helicopter, but the bullet goes towards the first helicopter.

    I assume this is because the helicopter is not selected in the "on shoot" event, so it just uses the first one, but how do I fix this?

  • 'Add object to target' simply adds an object type to look for, so you only need to call it once on Start of Layout. 'Spawn object' will set the angle of the spawned object to the angle of the parent object, which is exactly what you want - you don't need to set the bullet's angle manually. 'On shoot' doesn't pick any helicopters so Construct doesn't know which helicopter you want to use. Delete the set angle action and it should all work fine.

  • Thanks for your quick response, the problem is I was hoping to make the bullets chase the target, that's why I needed to find what the turret's aiming at in the shoot event, rather than just the angle.

    EDIT - actually, I suppose I could store the UID in a variable in the target acquired event, then find it again in the shoot event, is that the easiest way?

    EDIT 2 - actually, I think I have the same problem in that event too... am I best off doing it manually rather than using the turret behaviour?

  • So, let me see if I've got this straight...

    The turret fires Bullet A at Helicopter 1. The bullet is a homing bullet that chases the helicopter.

    Helicopter 1 moves out of range. Helicopter 2 moves into range. The turret changes it's target to Helicopter 2.

    So now you want Bullet A to change course and chase Helicopter 2? Is that correct?

  • So, let me see if I've got this straight...

    The turret fires Bullet A at Helicopter 1. The bullet is a homing bullet that chases the helicopter.

    Helicopter 1 moves out of range. Helicopter 2 moves into range. The turret changes it's target to Helicopter 2.

    So now you want Bullet A to change course and chase Helicopter 2? Is that correct?

    No, sorry, I want the turret to fire at the helicopter it's currently aiming at, from the "on shoot" event. The trouble is, the specific helicopter can't be determined within the "on shoot" event, only the angle, so I can't tell the bullet which helicopter it should be chasing.

  • I think you've got it right up to the end, deadeye. But if I'm reading it right he wants the bullet to keep chasing the first helicopter, even when the turret isn't targetting it, but for new bullets fired not to chase it.

    Because he's picking the first helicopter for the homing bullets and all bullets are globally homing on that target he can't have bullets chasing different targets.

  • I think you've got it right up to the end, deadeye. But if I'm reading it right he wants the bullet to keep chasing the first helicopter, even when the turret isn't targetting it, but for new bullets fired not to chase it.

    Because he's picking the first helicopter for the homing bullets and all bullets are globally homing on that target he can't have bullets chasing different targets.

    Yes, that's right.

    If the event was "on shoot at (object)", so in my example "on shoot at helicopter", then I could make the bullet follow that helicopter, or apply actions directly to the targeted helicopter, making the turret behaviour a lot more flexible.

    So my question is whether that can already be done some other way.

  • Well, you're right, there's no easy way to access the targeted object from the turret movement. To remedy this I've added a 'Get Target UID' expression to the turret movement for the next build. You'll be able to do something like this:

    + On shoot

    : Spawn bullet

    : Set bullet private variable to Gun[Turret].TargetUID

    + For each helicopter

    + Bullet's variable = helicopter.UID

    : Set angle toward helicopter

    Then every time a bullet is shot, it will lock on to the helicopter it was shot at, and follow it.

  • Well, you're right, there's no easy way to access the targeted object from the turret movement. To remedy this I've added a 'Get Target UID' expression to the turret movement for the next build. You'll be able to do something like this:

    + On shoot

    : Spawn bullet

    : Set bullet private variable to Gun[Turret].TargetUID

    + For each helicopter

    + Bullet's variable = helicopter.UID

    : Set angle toward helicopter

    Then every time a bullet is shot, it will lock on to the helicopter it was shot at, and follow it.

    That's exactly what I'm after, thanks a lot! Looking forward to the next build.

  • Ah, I see. I've been tinkering around with it for a bit and I'm coming up with the same problem you're having. All of my homing bullets follow the first helicopter.

    Maybe a new feature request for the Turret behavior? Like "Get Target UID?" Then you could do something like this, maybe:

    + Turret has target

    • Turret: Spawn object Bullet
    • Bullet: Set value('myTarget') to turret.TargetUID

    + Bullet: Value 'myTarget' Greater than 0

    + Group: Enemy Unique ID is bullet.Value('myTarget')

    - Rotate 5 degrees toward Group: Enemy

    Edit: Sweet Jesus, was I ever beaten. I've got to learn to check the thread again before hitting "submit."

  • ONE more question, sorry... is it possible to find whether a UID is valid? I'm just wondering what happens if a bullet is fired, but then the helicopter is destroyed by something else while the bullet is en route.

    + For each helicopter
    + Bullet's variable = helicopter.UID
    : Set angle toward helicopter 
    [/code:sai1utxl]
    
    So here, obviously the "set angle toward helicopter" won't apply if the helicopter is gone, but is there a way to say "and if the helicopter UID is no longer valid, delete the bullet"?
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Well, because the event won't run when the helicopter with intended UID is destroyed, you can use event logic something like this:

    + Always

    : Bullet: set 'Is obsolete' to 1

    + For each helicopter

    + Bullet's variable = helicopter.UID

    : Set angle toward helicopter

    : Set 'Is obsolete' to 0

    + Bullet: 'Is obsolete' = 1

    : Destroy bullet

    And lol deadeye, great minds think alike hey

  • Well, because the event won't run when the helicopter with intended UID is destroyed, you can use event logic something like this:

    + Always

    : Bullet: set 'Is obsolete' to 1

    + For each helicopter

    + Bullet's variable = helicopter.UID

    : Set angle toward helicopter

    : Set 'Is obsolete' to 0

    + Bullet: 'Is obsolete' = 1

    : Destroy bullet

    And lol deadeye, great minds think alike hey

    OK, thanks again everyone for their help.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)