Making a timed laser

This forum is currently in read-only mode.
From the Asset Store
Make your own platformer for both the web and mobile easy with this Santas Platformer Template, FULLY DOCUMENTED
  • Hello!

    I was hoping to get some help in making a level with a timed laser for a flying level. I want to have some sort of gun in a fixed spot, always aiming at the player and at regular intervalls it should shoot at the player. Now, the gun will always fire at the spot the player is currently at, so as long as the player keeps moving the laser will never actually hit, because the player will have moved on when the laser arrives. Hope that made sense!

    How would I go on about designing something like that?

    (everything else about the level is completed, hit detection, player movement etc )

  • What I would try to do is first and foremost, use the event under System called "Every X Milliseconds"

    And then have it like

    Player is On-Screen

    Every 1000 milliseconds (1 second- you can have this anything you want)

    ---->EnemyTurret Spawn object "Laser" on LayerWhatever

    ---->Laser Set Angle towards Object Player

    Now you need to give the object Laser the Bullet Behavior, and set its speed accordingly. Make sure its rotation count is at 8, but that shouldn't be a problem because that's the default.

    You might want to have a condition that makes it so that the Turret stops firing when your ship is too far away from the Turret.

    You'd do that by using the System command Compare Values, and then

    abs(Turret.X - Player.X) is Lower Than 500 (or whatever distance). This is not necessary, but it makes things neater and you don't have a turret constantly firing.

  • Hey there, just as an additional thought;

    If you wanted to make it so each gun fired at a different time to each other, but still at regular intervals (assuming you have multiple guns going), you could always give the gun object a private variable called like 'Timer' and do something like this (semi-pseudocode):

    if player is within range of the gun

    --->add timedelta to gunthing.Value('Timer')

    if gunthing.Value('Timer') is greater or equal to 1000

    -->Do your shooting code here

    -->Set gunthing.Value('Timer') to 0

    if player is out of range of gun

    -->Set gunthing.Value('Timer') to 0

    (or if you want the gun to fire as soon as you're in range)

    --->Set gunthing.Value('Timer') to 999

    I know it's probably pretty sloppy and I haven't actually tested it, but it should work close enough, obviously you'd use something like the system object's 'calculate distance between objects' condition under math (i think that's what it's called) to determine the range of the gun.

    Also i -think- 1000 is the value for one second, so if you wanted two seconds, you would replace that value with 2000 or whatever suits your fancy. The best approach would be to make a global variable called something like 'gunInterval' and use its value instead of rewriting code.

    If you have questions about all that garbage I just said, feel free to ask

  • Thank you everyone, I think I have managed to piece something together from your advice. However, one problem still remains:

    As you can see, the gun follows the player just fine, but for some reason it is facing the wrong way. Any idea on what I have done wrong?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • You could have drawn the gun facing left, if you draw it facing right, it should work.

    Construct treats facing right as the 'default' rotation

  • Thanks again!

    I have everything more or less figured out now

    However, during playtesting I have found that the player has too little time to react once the laser is fired. What I�d like to do is have the turrent "blink" for a while (I already has sprites for this animation) and stop tracking the player for a second before it fires.

    Anyone knows how I could do something like that?

  • You could probably change the firing event into the 'blink' event. This is a pretty long post so I'll bold the more important stuff in my rants.

    So if we use something similar to my previous example:

    if player is within range of the gun

    --->Look in the direction of the player

    <subevent>gunthing animation Blink is not playing

    ------>add timedelta to gunthing.Value('Timer')

    if gunthing.Value('Timer') is greater or equal to 1000

    if gunthing animation Blink is not playing

    --->Change gunthing animation to blink

    --->Set gunthing.Value('Timer') to 0

    If gunthing is playing 'Blink' animation

    --->add timedelta to gunthing.Value('Timer')

    <subevent>if gunthing.Value('Timer') is greater or equal to 1000 (or however long you want the blink to last)

    --->Inset your gun shooting code here

    --->Set gunthing.Value('Timer') to 0

    Once again, I've been naughty and not actually tested this, but this where I would begin if I was about to try and achieve what you're after. To achieve the 'if animation blink is not playing' condition, add a condition 'If animation blink IS playing', then right click on it and click on 'invert condition'. This makes it so the event triggers when the OPPOSITE of what is listed in the condition is true.

    I don't know how much you know about subevents, but I'm using two there, that's where you either click on one event, click on it again and then select, 'create new subevent' or you drag an existing event underneath and to the side of another existing event (so it's indented). There's probably smoother ways to do this code, but I made it so the blink has its own timer check too in case you want it shorter, like 500.

    The thing with subevents, they behave exactly like a normal event, except that the event that they're indented from has to also be true. I basically threw that in there for two reasons, I'm so used to doing it these days, and it cuts down on having the same condition in multiple events:

    For example:

    Instead of this:

    If player is running

    if jump is pressed

    --->do this

    If player is running

    If fire is pressed

    --->do something else

    You can condense this using subevents (it may seem like not a whole lot at the moment, but in larger projects it really helps):

    If player is running

    <subevent>If jump is pressed

    ------>Do this

    <subevent>If fire is pressed

    ------>Do something else

    The advantage, besides saving space, you can also use the event 'If player is running' in the above example to also have its own action too when neither jump or fire is pressed and just running is playing, if that makes any sense.

    I hope this has helped and that I've said it in an understandable manner Sorry if it's miles long haha. If you have further questions or need me to translate or illustrate what I mean, I'll be happy to help.

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