Kour.io

將玩家的視角調整對準敵人頭部

// ==UserScript==
// @name        Kour.io
// @license     MIT
// @match       *://kour.io/*
// @grant       none
// @description 將玩家的視角調整對準敵人頭部
// @version     1.0
// @author      HUANG WEI-LUN HUANG WEI-LUN
// @namespace https://greasyfork.org/users/1498412
// ==/UserScript==
// 模擬一個自動瞄準最近敵人頭部的功能

// 假設你有一個玩家對象和一組敵人對象
const player = {
    x: 100,
    y: 0,
    z: 100,
    rotation: { yaw: 0, pitch: 0 }  // 玩家目前的視角
};

const enemies = [
    { x: 120, y: 1.8, z: 90 },   // 頭部高度通常在 1.8 左右
    { x: 80, y: 1.8, z: 130 },
    { x: 110, y: 1.8, z: 105 }
];

// 計算兩點之間的距離
function getDistance(a, b) {
    return Math.sqrt(
        Math.pow(b.x - a.x, 2) +
        Math.pow(b.y - a.y, 2) +
        Math.pow(b.z - a.z, 2)
    );
}

// 尋找最近的敵人
function getClosestEnemy(player, enemies) {
    let closest = null;
    let minDist = Infinity;

    enemies.forEach(enemy => {
        const dist = getDistance(player, enemy);
        if (dist < minDist) {
            minDist = dist;
            closest = enemy;
        }
    });

    return closest;
}

// 將玩家的視角調整對準敵人頭部
function aimAtTarget(player, target) {
    const dx = target.x - player.x;
    const dy = target.y - player.y;
    const dz = target.z - player.z;

    const yaw = Math.atan2(dz, dx) * 180 / Math.PI - 90;
    const distanceXZ = Math.sqrt(dx * dx + dz * dz);
    const pitch = -Math.atan2(dy, distanceXZ) * 180 / Math.PI;

    player.rotation.yaw = yaw;
    player.rotation.pitch = pitch;

    console.log(`鎖定目標視角:Yaw=${yaw.toFixed(2)}°, Pitch=${pitch.toFixed(2)}°`);
}

// 模擬運行
const target = getClosestEnemy(player, enemies);
if (target) {
    aimAtTarget(player, target);
} else {
    console.log("沒有目標");
}