How can I make the entity walk to the player and not be teleported directly?
if (response.selection === 0) {
if (entity.hasTag("trader:Ui")) {
entity.removeTag("trader:Ui");
entity.addTag("trader:walk");
entity.playAnimation("animation.trader.happy");
entity.runCommandAsync("event entity @s jm:test");
const playerPosition = player.location;
const playerDirection = player.getViewDirection();
const horizontalDirection = {
x: playerDirection.x,
z: playerDirection.z
};
const length = Math.sqrt(horizontalDirection.x * horizontalDirection.x + horizontalDirection.z * horizontalDirection.z);
horizontalDirection.x /= length;
horizontalDirection.z /= length;
const scale = 1;
const newX = playerPosition.x + horizontalDirection.x * scale;
const newY = playerPosition.y;
const newZ = playerPosition.z + horizontalDirection.z * scale;
entity.teleport({ x: newX, y: newY, z: newZ }, entity.dimension);
}
}