public class FollowerManager : MonoBehaviour
{
[SerializeField]
private int _followerDistance;
public Follower leader;
private Vector2 lastLeaderPosition;
public List<Follower> followerList = new List<Follower>();
private List<Vector2> followerPath = new List<Vector2>();
private void Start()
{
AllocateFollowerPath();
}
private void FixedUpdate()
{
// update position of each follower
for (int i = 0; i < followerList.Count; i++)
{
followerList[i].transform.position = followerPath[_followerDistance * (i + 1)];
}
ShiftFollowerList();
}
private void ShiftFollowerList()
{
Vector2 currentPos = leader.transform.position;
if (currentPos == lastLeaderPosition || followerPath.Count == 0) return;
// This is responsible for shifting the entire array as the player continues to move
for (int i = followerPath.Count - 1; i > 0; i--)
{
followerPath[i] = followerPath[i - 1];
}
// Set the new position and record last position
followerPath[0] = currentPos;
lastLeaderPosition = currentPos;
}
private void AllocateFollowerPath()
{
int target = (_followerDistance * (followerList.Count + 1)) - followerPath.Count;
// Only allocate the necessary amount of spaces for the followers
for (int i = 0; i < target; i++)
{
followerPath.Add(transform.position);
}
}
private void DeallocateFollowerPath()
{
int target = ((followerList.Count + 1) * _followerDistance) - _followerDistance;
for (int j = followerPath.Count - 1; j > target; j--)
{
followerPath.RemoveAt(j);
}
}
}
Hey all. I'm currently working on an Earthbound-inspired JRPG follow script for my game, and I've been having some issues with getting the follow quite right.
What I'm trying to do is make each follower in the party mimic the players exact movements while following behind at a distance (similar to games like Deltarune or Earthbound)
While the code above does mimic the movements of the leader, the issue is that the followers stutter and jitter (which is very noticeable when the camera is following the leader). I had a feeling this has to do with update loops, so I've tried moving around the order of my code but I haven't had any luck with it.
A couple notes:
- The leader of the party hard sets the velocity of its rigidbody in a FixedUpdate loop.
- The camera movement strictly follows the leader without any smoothing applied in a LateUpdate loop (this makes it so there's no camera jitter when locked onto the leader)
- I'm not using cinemachine
- I've tried hard setting script execution order to apply all movement before the camera follows, but it changing execution order doesn't seem to change anything
- I've tried ShiftFollowerList() in LateUpdate and Update, but this seems to make the followers frame dependent
Not really sure what to do, I've been stuck on this for a bit. If you have any ideas or need clarification please let me know, I would greatly appreciate the help! ^^