Bullet's angle is offset when shot towards the cursor

Hey guys! So, I’ve been working on an endless runner/shooter hybrid, and I surprised myself with a pretty cool mechanic. I managed to code an aiming hand that rotates to follow the cursor, but when I want to shoot (left click) bullets towards the cursor, the bullet always goes higher it. This only happens if the angle isn’t a perfect 90 degrees, and it makes shooting enemies kinda wonky.

For reference, this here is the shooting code:
image

I have used the FireBullet extension which I’ve grown pretty fond of, with a 0.8 FiringCooldown. The bullet is fired from the “Muzzle” point, which is the right end of the AimHand.

And to clarify a bit more, here’s the code for the rotating arm:
image

Overall, what could be happening for the bullet to not follow the trajectory? Do I have to tweak some values in the extension? I’d prefer not to get rid of it since it works pretty well, just need to adjust some angles.

If y’all need anymore screenshots, feel free to ask, and thanks in advance!

How much higher? Are the mouse/cursor and bullet on the same layer? If not, is there some scrolling happening?

Yup, they’re all in the same layer.

The distance between cursor and bullet trajectory depends. The steeper the angle is, the higher the bullet will go.

I’ve tried taking some screenshots to give you a good idea. Just follow the cursor.

As you can see, the closer the cursor is to that straight 90 degree angle, the smaller the offset is.

Oh, that’s quite a large discrepancy. I was expecting about a bullet’s height difference.

Does the game scroll left/right? If it does, does the wayward trajectory still happen at the very start of the scene, before any scrolling has occurred?

If there’s no scrolling, then try using the fire bullet action at an angle, using the angle between objects - the AimHand and the Cursor.

Okay, woah. You were right on the money with the scrolling!

Yeah, I have the player coded to always simulate pressing right, since it’s an endless runner. I disabled that, and the bullet more or less goes in the direction of the cursor.

So the issue is definitely with the scrolling.

What’s happening is that the bullet is going to the cursor position at the time of the mouse click. However, that position is moving to the left as the game scrolls, and while the bullet is moving correctly, visually it looks weird.

If the player is always moving right, you could recalculate the targeting position based on current distance and scrolling speed.

I’m not sure exactly what algorithm to use, but I think it’ll involve getting the current Y distance, working out how long it would take to scroll that far (given the player’s move speed) and multiplying that figure with the bullet speed to get the new target y position.

Welp, that way it seems I have my work cutout for me.

But I think I’ll try a bit of trickery. I’ll make a custom cursor that follows the mouse’s, put it on another layer (so the X and Y don’t change), and see if it works that way. I’ll keep you posted.

If not, I’ll try to figure out an algorithm.

1 Like

Yeah, no. I actually thought I was slick there for a second.

If I try to put a crosshair on a new layer, then code it to follow the position of the cursor ON ANOTHER LAYER, it just keeps going to the right. And if I put the crosshair on the same layer as the cursor…then I just have the same issue.

So I have to figure out an algorithm, like you said.

It will work if you add the difference between the camera centres of the two layers to the CursorX(). Make it scrolling layer CameraX() - cursor layer CameraX().

Ok, yeah, that makes sense. But would I add that as a different action in the same event, or a completely new event?

image

So far I have this, and the CrossHair is on a different layer.

In the meantime, I’ve tried to come up with an algorithm as you said, and stuck to the DistanceBetweenPositions expression:

image

It’s taken quite a bit of trial and error, but it’s somewhat reliable. It still acts up a bit in the 45-90 degree range, but it’s good enough for me.

I’m being real nitpicky all of a sudden, but I’ve noticed how all the moving parts jitter, like the crosshair and AimHand. This is definitely a result of the scrolling once again, but how could I make the movement smoother? Would lerps help?

How are you setting the AimHand position? And how are you scrolling the scene or moving the camera?

Well first of all, I am setting the crosshair up to follow the cursor in the base layer. For the AimHand, I’ve attached it to a point on the character, with a lower Z-Order, so it looks like the character is freely moving his arm.

Here is the code for both:

The camera is set up to follow the player, with a small offset at the X and Y points, in order to have a lot of space to see the obstacles and enemies as they appear:

And since the camera follows the player anyway, the scrolling is done simply by always simulating a right key press:

image

Do you simulate the keypress before or after the AimHand and crosshair positioning? Try making the simulate right key press the last event of the event sheet and see if that makes a difference.

The other thing is you could try is check the round pixels option in the game properties.

It doesn’t seem to have made much of a difference. I’m thinking, however, but I think that this is a cop out answer, that it might have something to do with how GDev runs on my PC. I had another game which I made on my laptop, with an integrated GPU, which would run pretty well. But on this desktop PC, with a dedicated GPU and some actual horsepower, games would always be clunky. The program itself would be a little slower. I’m thinking this might be it with this game.

Anyways, I think we’ve gotten about as close to a solution as we can in this thread, and I’m satisfied with what I have right here. Thanks a lot for your time, and your insight, especially regarding your answer about the cursor position and offset. You really got me to think out of the box. As always, thanks a lot for your help, you’ve been very insightful!

1 Like

Sorry if its not allowed to bump old threads!

I have found a fix for a issue like this, I guess!
My crosshair was misaligned as well as the bullets that came out of the gun:

I tweaked the points (center, origin) of the gun & the character’s “GunAttachmentPoint” so now it works (yellow “bean” = bullet lolz):

Don’t mind the fact the character is hovering in the first picture, this has nothing to do with the bug/error/issue

Could it be that you didn’t set the gun extension to not rotate the bullet?