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.
// ======================================================
// ๐ฑ๏ธ 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;
}
}
obj.speed.arriveDistance sets when the destination is considered reached.mouse.x, mouse.y, mouse.down from the global system.Math.atan2(dy, dx) and apply it to obj.rotation.