MZ Party Follower diamond formation

Hello, back in the day, I found a way to have interesting followers on RPG Maker VX Ace.
test.gif

Today, I tried to recreate this on MZ, but the result wasn't very convincing.

Code:
const formation = [
  [-1, 0], // Follower 1 à gauche
  [1, 0],  // Follower 2 à droite
  [0, -1], // Follower 3 en haut
  [0, 1],  // Follower 4 en bas
];

const dir = $gamePlayer.direction();

for (let i = 0; i < $gamePlayer.followers()._data.length; i++) {
  const follower = $gamePlayer.followers().follower(i);

  if (!follower._formationTarget) {
    follower._formationTarget = {x: $gamePlayer.x, y: $gamePlayer.y};
  }

  let dx = formation[i][0], dy = formation[i][1];
  let offsetX = dx, offsetY = dy;
  if (dir === 2) { offsetX = dx; offsetY = dy; }
  else if (dir === 4) { offsetX = -dy; offsetY = dx; }
  else if (dir === 6) { offsetX = dy; offsetY = -dx; }
  else if (dir === 8) { offsetX = -dx; offsetY = -dy; }
  const newTargetX = $gamePlayer.x + offsetX;
  const newTargetY = $gamePlayer.y + offsetY;


  if (follower.x === follower._formationTarget.x && follower.y === follower._formationTarget.y) {
    follower._formationTarget = {x: newTargetX, y: newTargetY};
  $gamePlayer._followers.updateMove = function() {};
}


  let direction = 0;
  if (follower.x < follower._formationTarget.x) direction = 6;
  else if (follower.x > follower._formationTarget.x) direction = 4;
  else if (follower.y < follower._formationTarget.y) direction = 2;
  else if (follower.y > follower._formationTarget.y) direction = 8;

  if (direction && follower.direction() !== direction) {
    follower.setDirection(direction);
  } else if (direction) {
    follower.moveStraight(direction);
  }
}

test2.gif

It might not be obvious in the gif, but the followers move jerkily, and they also walk on tiles that are supposed to be impassable...


As you can see, I don't know how to code.
If a kind soul could help me, I’d really appreciate it.
Thanks in advance 🙂
Personally, I think you're very close. You just need to check for tile passability before you do the "moveStraight" command. RPG Maker already comes with default:

JavaScript:
$gameMap.isPassable(x, y, direction)

The "direction" in this case refers to the tile's passability direction, not the character's. (Like passable only from above, below, etc.)

Original thread: https://forums.rpgmakerweb.com/threads/party-follower-diamond-formation.178119/