For some reason, when the NPC reaches directly behind the player, the position of the NPC resets to the start position. This shouldn't happen. Can anybody help me?
PHP Code:
function onCreated() {
setshape(1, 32, 32);
this.speed = 12;
//format: idle animation, walking animation
this.ganis = {"idle", "walk"};
showcharacter();
setCharAni("idle", null);
setTimer(0.1);
}
function onPlayerChats() {
switch (player.chat) {
case ":destroy":
destroy();
break;
case ":follow":
this.following = true;
setTimer(0.1);
break;
case ":stop following":
this.following = false;
setTimer(0.1);
break;
}
}
function onTimeout() {
this.nick = this.attr[5] @ "'s Pet";
if (this.following == true) {
//get the coordinates that are directly behind the owner
for (pl: players) {
if (pl.account == this.attr[5]) {
temp.newxy = {
pl.x + 0.5 - (vecx(pl.dir) * 3),
pl.y - (vecy(pl.dir) * 3)
};
//pl.chat = vecx(player.dir) SPC vecy(player.dir) SPC player.dir;
break;
}
}
//work out delta_x and delta_y for move
temp.dxy = {
temp.newxy[0] - this.x,
temp.newxy[1] - this.y
};
this.dir = getdir(temp.dxy[0], temp.dxy[1]);
//set the animations
if (this.x == temp.newxy[0] && temp.y == this.newxy[1]) {
if (this.ani != this.ganis[0]) {
setCharAni( @ this.ganis[0], null);
}
}
else {
if (this.ani != this.ganis[1]) {
setCharAni( @ this.ganis[1], null);
}
}
//work out the distance and time to cover that distance
temp.dist = getDistance(temp.dxy[0], temp.dxy[1]);
temp.time = temp.dist/this.speed;
move(this.dxy[0], temp.dxy[1], temp.time, 4);
if (temp.time < 0.1) temp.time = 0.1;
setTimer(temp.time);
}
}
function getDistance(temp.x, temp.y) {
temp.dist = (temp.x ^ 2 + temp.y ^ 2) ^ 0.5;
return temp.dist;
}