Blog / Tutorial
Phaser 4: Aim Through the Barrel So Bullets Hit the Cursor
Rotating a gun sprite to the mouse aims the grip, not the barrel, so shots miss. Here is the two-line fix for Phaser 4, with a live demo.
You point a gun sprite at the mouse, click, and the bullet sails just past the cursor. Every time. Move closer and it misses by the same amount.
It's not your bullet code. It's where the gun is rotating from.
Try the live demo, pick the pistol, rifle or shotgun and aim anywhere. The shots go straight through the cursor. This post shows how.
Why the shots miss
Every Phaser game object rotates around its origin. For a gun held in a hand, the origin is the grip. That's what you want for rotation, because the gun should pivot in the hand.
But the barrel isn't at the grip. On most gun sprites the barrel sits higher than the grip, so the line the bullet travels along is shifted up from the line between the grip and the cursor.
The usual aiming code looks like this:
const angle = Phaser.Math.Angle.Between(gun.x, gun.y, pointer.worldX, pointer.worldY);
gun.setRotation(angle);
That points the grip at the cursor. The barrel ends up parallel to that line, so every shot misses by exactly the barrel's height above the grip. Twenty pixels of offset is a twenty-pixel miss, at any distance.
The fix: tilt by the muzzle offset
You need two numbers:
dist: the distance from the grip to the cursor.off: how far the muzzle sits above (negative) or below (positive) the grip, in screen pixels, measured at right angles to the barrel.
Then rotate a little further, so the line out of the barrel passes through the cursor:
const dx = pointer.worldX - gun.x;
const dy = pointer.worldY - gun.y;
const dist = Math.hypot(dx, dy);
const phi = Math.atan2(dy, dx); // angle from the grip to the cursor
const a = dist > Math.abs(off) ? phi - Math.asin(off / dist) : phi;
gun.setRotation(a);
Why asin? Picture the cursor at distance dist from the grip. The barrel's line runs parallel to the gun, off pixels to one side. For that line to pass through the cursor, the cursor has to sit off pixels to the side of the gun's direction, and that happens when sin(phi - a) = off / dist. Solve for a and you get the line above.
The dist > Math.abs(off) check covers the cursor sitting right on top of the gun, where no angle can work. The code just falls back to pointing at it.
Getting off from your sprite
If your atlas stores a pivot and a muzzle point per frame (Retro Arsenal does, in its JSON), off is the vertical gap between them, scaled to screen size:
const c = gun.frame.customData.custom; // pivot_px and muzzle_px from the atlas
const off = (c.muzzle_px.y - c.pivot_px.y) * Math.abs(gun.scaleY);
No atlas data? Open the sprite in any image editor, read the y position of the grip and the barrel, and use the difference times your scale.
Don't forget the flip
When the gun points left, you flip it vertically so it isn't upside down. Flipping also flips which side the barrel is on, so off changes sign:
const flip = Math.abs(phi) > Math.PI / 2 ? -1 : 1;
const offNow = off * flip;
const a = dist > Math.abs(offNow) ? phi - Math.asin(offNow / dist) : phi;
gun.setRotation(a);
gun.setScale(baseScale, baseScale * flip);
Skip this and aiming is perfect on the right side of the screen and off on the left.
Fire from the muzzle
Aiming through the barrel only helps if the bullet starts at the barrel too. The previous post covers working out the muzzle's world position from the atlas data: Aiming and firing from the muzzle. Spawn the bullet there, send it along gun.rotation, and it lands on the cursor.
The whole thing
aimAt(pointer) {
const dx = pointer.worldX - this.gun.x;
const dy = pointer.worldY - this.gun.y;
const dist = Math.hypot(dx, dy);
const phi = Math.atan2(dy, dx);
// Pointing left? Flip so the gun isn't upside down.
const flip = Math.abs(phi) > Math.PI / 2 ? -1 : 1;
// Muzzle height above the grip, in screen pixels, on the side it's on now.
const c = this.gun.frame.customData.custom;
const off = (c.muzzle_px.y - c.pivot_px.y) * this.baseScale * flip;
// Aim the barrel, not the grip.
const a = dist > Math.abs(off) ? phi - Math.asin(off / dist) : phi;
this.gun.setRotation(a);
this.gun.setScale(this.baseScale, this.baseScale * flip);
}
Call it from pointermove, and again after anything that changes the gun's frame or scale, like a tier upgrade.
Does it really work?
The live demo uses this exact maths for all three guns, each with a different muzzle height. Press D in the demo to see every pivot and muzzle point while you aim.
I also checked the formula with a quick test over 100,000 random cursor positions and muzzle offsets. The worst miss was a rounding error, far below a pixel. Without the fix, the miss always equalled the muzzle offset.
Get the sprites
The free laser pistol from the first tutorial already has the pivot and muzzle data in its JSON, so this code works on it as-is.
The rifle, shotgun, energy blade and plasma grenade, each in three tiers with the same data, are in Retro Arsenal. I made it, so I'm biased, but it's built for exactly this.
How this post was made: Written by Khaya, drafted with help from Claude (AI). Khaya tested the aiming in the live demo before publishing. The aiming code is the same code running in the live Retro Arsenal demo, and the maths was checked with a numeric test over 100,000 random aim positions.