Simple custom path modifier.
using UnityEngine;
using System.Collections.Generic;
using Pathfinding;
public class ModifierTutorial : MonoModifier {
public override int Order { get { return 60; } }
public int iterations = 5;
public int subdivisions = 2;
public override void Apply (Path path) {
if (path.error || path.vectorPath == null || path.vectorPath.Count <= 2) {
return;
}
subdivisions = Mathf.Max(subdivisions, 0);
if (subdivisions > 12) {
Debug.LogWarning("Subdividing a path more than 12 times is quite a lot, it might cause memory problems and it will certainly slow the game down.\n" +
"When this message is logged, no smoothing will be applied");
subdivisions = 12;
return;
}
List<Vector3> newPath = new List<Vector3>();
List<Vector3> originalPath = path.vectorPath;
int subSegments = (int)Mathf.Pow(2, subdivisions);
float fractionPerSegment = 1F / subSegments;
for (int i = 0; i < originalPath.Count - 1; i++) {
for (int j = 0; j < subSegments; j++) {
newPath.Add(Vector3.Lerp(originalPath[i], originalPath[i+1], j*fractionPerSegment));
}
}
newPath.Add(originalPath[originalPath.Count-1]);
for (int it = 0; it < iterations; it++) {
for (int i = 1; i < newPath.Count-1; i++) {
Vector3 newpoint = (newPath[i] + newPath[i-1] + newPath[i+1]) / 3F;
newPath[i] = newpoint;
}
}
path.vectorPath = newPath;
}
}