I just can't find what is causing this bug to happen
function arrestEnemy(player) {
const entitiesInView = player.getEntitiesFromViewDirection({ maxDistance: 4 })
if (entitiesInView.length > 0) {
const nearestEnemy = entitiesInView[0]
const playerPos = player.location
const playerRotation = player.getRotation()
const offsetX = 1.5 * Math.sin((playerRotation.y + 90) * (Math.PI / 180))
const offsetZ = 1.5 * Math.cos((playerRotation.y + 90) * (Math.PI / 180))
const newX = playerPos.x + offsetX
const newZ = playerPos.z + offsetZ
const newY = playerPos.y
nearestEnemy.runCommand(`tp @s ${newX} ${newY} ${newZ}`)
}
}