[Solved] How do I fire a bullet at Cursor X,Y position and destroy it there?


What is the actual result

Right now I can fire a bullet and it just flies towards a wall ignoring my condition.
I have a bulletPosX and bulletPosY number variables on bullet object.
I’m changing those variables to bulletPosX = cursorX, bulletPosY = cursorY at the moment the bullet is fired. This is done in repeat for each instance of the bullet.

After that I’m checking if bullet current X and Y positions are equal to bulletPosX and bulletPosY and if so Destroy bullet object. That doesn’t work?
I tried moving this check to “repeat for each instance” etc. but that also doesn’t work.

I also have a check against wall collision and destroy object works without any issues there.

I’d really appreciate any help with this. I feel very stupid especially after finding the same problem on forums I still can’t get this thing to work.

Related screenshots

Events, Bullet object and Game view with “validator” :slight_smile: First set of values are X,Y of the mouse cursor at the moment of firing a bullet. Second set of values are a variables X,Y on a bullet.

Hi there,

First off, you shouldn’t put the For Each as a subevent to mouse release, this will change the variable for all the bullets every time a new bullet is fired.

That’s because bullets don’t move with 1 pixel increments on each frame. It depends on the firing speed and angle. So it’s highly unlikely the bullet will land on the exact position of the cursor and would skip it between frames. Thus the condition is not met.

Walls have dimensions so bullet would probably collide with it and not skip it between frames (unless the bullet is traveling at a very high speed).

Try this:

1 Like

It works! That’s a hefty expression for me :slight_smile: I need to read about that. Thank you for your help!

I would recommend using an object position tween. It’s precise and accurate each and every time. You can set it to delete the object at the end or use tween has finished You wouldn’t need the variables.

Create object
Tween object position CursorX(), CursorY()


Downside, you’d have to handle the fire timing and such. So, it might not be your best choice if the gun needs to be loaded or such.

1 Like

No problem!

It’s a simple expression, I’ll try to explain it.

Basically when a bullet is fired, create variables for it with the starting position (pos.X & pos.Y) and cursor’s position (destination.X & destination.Y). The boolean is to prevent new bullets from being picked.

Then compare the distances, if (pos ↔ bullet) is larger than (pos ↔ destination), this means that the bullet traveled to the cursor’s position.

1 Like