#get falldistance

1 messages · Page 1 of 1 (latest)

exotic grotto
#

how to do this

worn marsh
#

isn't there Player.fallDistance?

shadow jasper
#

Use Entity.isFalling to check if they're falling, each tick.

Initially, false would save their current Y position. After transitioning from true back to false, subtract the old Y position with the current one, which returns the fall distance.

shadow jasper
worn marsh
#

I remember there was a fallDistance property

#

maybe I'm just remembering wrong

exotic grotto
worn marsh
exotic grotto
exotic grotto
#

anyone?

shadow jasper
exotic grotto
alpine turret
#
function getFallDistanceAsync(player: Player) {
    return new Promise(async (resolve) => {
        if (!player.isFalling) {
            resolve(0)
            return
        }
        const startY = player.location.y
        while (player.isFalling) {
            await system.waitTicks(2)
        }
        resolve(startY - player.location.y)
    })
}```
```ts
async function playerTracker(player: Player) {
    player.sendMessage("Starting fall tracker for the player")
    const fallDistance = await getFallDistanceAsync(player)
    player.sendMessage(`I fell ${fallDistance} blocks!`)
}```
shadow jasper
#
const yChange = {};
const startingYCoord = {};
system.runInterval(()=>{
    world.getPlayers().forEach((player)=>{
        const currY = player.location.y;
        
        const onGround = player.isOnGround;
        const moveUpwards = player.getVelocity().y>=0;
        
        if (!moveUpwards)
        delete yChange[player.id]
        
        if (onGround || moveUpwards) {
            const oldY = startingYCoord[player.id] ?? currY;
            if (oldY-currY>0 && !yChange[player.id] && !player.isFlying) {
                breakFallEvent(player,oldY-currY);
                yChange[player.id] = true;
            }
            
            startingYCoord[player.id] = currY;
        }
    })
})

function breakFallEvent(player,fallDistance) {
    player.onScreenDisplay.setActionBar("FallDistance: §e"+fallDistance)
}
#

This version takes into account that if the player stops falling, or possibly levitates upwards mid-air... then, it resets the Y starting point.