[SOLVED] Make the bullets move towards the actual position (X,Y) of an object instead of the default position

I’m creating two different enemies for my platform game. Both must fire bullets toward the player’s position. But just one of them is working properly.
The bullets fired by Enemy2 always move towards the actual X,Y position of the RobotLower (the player object). But the bullets fired by Enemy3 apparently move towards the default X,Y position of RobotLower. It seems that it considers the top left corner of the sprite as the object position.
The code for both Enemies are the same. They both have the same “fire bullets” behaviors. I’ve checked the X and Y for every object in my game and all of them has their Origin point in their Center points.
For Enemy3, using expressions, I’ve tried to make the bullets move toward the player’s bounding box center point, the center point and origin points themselves (using PointX() and PointY()) but none of these worked. I tried just correcting the target by adding the number of pixels to X and Y and it kinda worked, but still I have no idea why this work around is needed for Enemy3 but not for Enemy2.
The code can be seen in the image attached.
I have no idea why the bullets fired by Enemy2 move to the player’s center point but not the ones fired by Enemy3. Could it be a bug?
I really hope someone can help me.

Including the bullet object?

What happens if enemy2 & enemy3 are firing from the same position? Does the problem still exist?

1 Like

Even the bullets, all of them have their origins in the center.
I tried placing both enemies at the same position, and it’s the same thing. Enemy2’s bullets go directly to the player, Enemy3’s bullets go to somewhere slightly above the player.
I’ve just made more tests and I think I’ve figured it out.
Apparently, if you use the action fire bullet toward position and program the bullet to go from a point inside the Enemy to a point inside the Player, not everything goes as expected. The bullet is fired from the Enemy’s point you wanted, but it doesn’t aim to the chosen point inside the Player.
For example, if the bullet is fired from a point 10 pixels above the origin of the Enemy, it will aim to another point 10 pixels above the origin of the Player, no matter what.
If you make a point inside the player and name it, for example, “BullsEye”, then you aim the bullet to Player.PointX(“BullsEye”), Player.PointY(“BullsEye”), it will simply ignore the point and do the things described above.
So why does it work in Enemy2 but not Enemy3? Because Enemy2 fires from a point near the center/origin. Enemy3 fires from a higher point.
I don’t think it should happen that way, so I ended up with more questions than answers, but at least I can figure out a work around.

In the games you’ve made, have you done something like this: using the fire bullets extension to fire a bullet from a point (not the origin) inside an object to a point (also not the origin) inside another object? If you did it, how did it work?

I’ve never used the bullet extension. I’ve always written my own bullet routines.

What you could do is open the bullet extension in the extension editor to see what it does. It could well be a bug.

To open the extension in the editor, click on the 3 dots beside the extension name in the project panel, and select “Edit”.

1 Like

I just looked at the Bullet behavior. The action for fire to position uses the direction of given angle function. It calculates the angle from the object to the target but it doesn’t take into account the actual bullet start position for the from location. It calculates the angle from center the center point of the object. So, when it fires from the supplied point the direction is just slightly off. I added an action to display the angle it was using and it was about 8 degrees off depending on the points.

image

If the formula is changed to use the start position it seems to work
image

IDK who maintains the behavior. There’s a list of names.
image

1 Like

Thank you very much for taking a look at the Fire Bullet extension.
As a beginner, I don’t understand the code inside extensions very well, so I can’t edit them as much as I would like.
It’s good to know that it isn’t exactly a bug, but just the way it calculates the trajectory of the bullet.
For now, I’m just using the work around in the events sheet: for the enemies in which the firing point is distant from its center, I’ll add some value to X and Y of the target point, to fix the trajectory and aim to the player’s position. For example, writing the target point of the bullet like Player.X()+16, Player.Y()+8.
But I’ll study more about extensions anyway. I’ll be able to make better code if I learn how to edit them, I think.

1 Like

Now I understand how and why the problem happens. Although I won’t risk making editions in the extension, I can figure out a solution in the events sheet that will work for this game. So I can consider it solved.
Thank you all for taking the time to help me! :grin:

1 Like

If you use fire bullets in the direction of given angle then you can calculate the angle instead of the behavior doing it.

image

1 Like

I think I can do it. I’m gonna try it now and let you know if it works.

1 Like

I just tweeted with @VictrisGames. They said they said “Thanks for pinging me! I see what the problem is and I will work on an update.”

2 Likes

This worked perfectly well!! I didn’t know about the AngleBetweenPositions expression.
Thank you very much for your help!!

1 Like