Bullet range (Phaser 3)

Here’s a way to make bullets expire at a certain distance traveled, taking advantage of newVelocity.

It’s more efficient to expire based on physics time instead. Phaser v3.50.0 will make that easier.

function fireBullet (bullet, x, y, vx, vy)
{
    // …
    // Range (px)
    bullet.state = 300;
}

function updateBullet (bullet)
{
    bullet.state -= bullet.body.newVelocity.length();

    if (bullet.state <= 0)
    {
        bullet.disableBody(true, true);
    }
}

3 Likes