๐Ÿ–ฑ๏ธ Example Script โ€“ Point & Click Player Controller

This script moves the player toward the point where the user clicks, perfect for Adventure or isometric/top-down RPG games. It uses the properties speed, arriveDistance, targetX and targetY.

๐Ÿ’ก Tip: This controller works with the engineโ€™s global input system. You can combine it with walking animations or sprite rotation.

๐Ÿ“œ Full Code

// ======================================================
// ๐Ÿ–ฑ๏ธ Point & Click Player Controller (Adventure / RPG)
// ======================================================
// Moves the player toward the clicked point using the engine API.
// Uses properties: speed, arriveDistance, targetX, targetY.

function Start(obj) {
    obj.type = "point_click_player";

    if (obj.speed === undefined) obj.speed = 120;
    if (obj.arriveDistance === undefined) obj.arriveDistance = 4;
    obj.targetX = null;
    obj.targetY = null;
}

function Update(obj, dt) {
    const s = obj.speed ?? 120;

    // Detect click from the global input system
    if (mouse.down) {
        obj.targetX = mouse.x;
        obj.targetY = mouse.y;
    }

    // If no destination, exit
    if (obj.targetX === null || obj.targetY === null) return;

    const dx = obj.targetX - (obj.x + obj.w / 2);
    const dy = obj.targetY - (obj.y + obj.h / 2);
    const dist = Math.sqrt(dx * dx + dy * dy);

    if (dist > obj.arriveDistance) {
        const nx = dx / dist;
        const ny = dy / dist;
        obj.x += nx * s * dt;
        obj.y += ny * s * dt;
    } else {
        obj.targetX = null;
        obj.targetY = null;
    }
}

๐Ÿ“˜ Quick Description

๐Ÿง  Suggestion: if you want the character to rotate toward the target, compute the angle using Math.atan2(dy, dx) and apply it to obj.rotation.