Skip to content

Instantly share code, notes, and snippets.

@dcbriccetti
Last active September 10, 2020 21:42
Show Gist options
  • Save dcbriccetti/451aeca04f11bf9be5046ebbdba052c4 to your computer and use it in GitHub Desktop.
Save dcbriccetti/451aeca04f11bf9be5046ebbdba052c4 to your computer and use it in GitHub Desktop.
private Vector3 EnemyMovement(Transform closestUnit, Transform enemy) {
Vector3 enemyToUnit = closestUnit.position - enemy.position;
int maxManhattanDistancePerMove = 3;
int xDirection = Math.Sign(enemyToUnit.x);
int zDirection = Math.Sign(enemyToUnit.z);
int xSteps = (int) Math.Min(maxManhattanDistancePerMove, Math.Abs(enemyToUnit.x));
int stepsLeft = maxManhattanDistancePerMove - xSteps;
int zSteps = (int) Math.Min(stepsLeft, Math.Abs(enemyToUnit.z));
int dx = xDirection * xSteps;
int dz = zDirection * zSteps;
return new Vector3(dx, 0, dz);
}
@dcbriccetti
Copy link
Author

Surely there’s some mathy way to avoid all the sign and abs business.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment