So this is probably gonna be long, but I have a game that I am creating and the basic concept is that the player can control 3 different characters. When the other characters are not being controlled they follow the one that is being controlled. I have also made it so the the player can “drop off” so to speak the other characters that are following the leader. There can be any combination of the three characters following one another.
My issue currently is with the script I am using for the actual following mechanic. Right now my script creates an array of the current leader’s positions and any character that is set to follow the leader will mimic the leaders movements with a delay. Here is that script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class delayTracker : MonoBehaviour
{
public Transform tracker = null;
public float delayTime;
private List<trackerPos> trackerPoses;
private int bufferSize = 100;
private float fps;
private int counter = 0;
void Awake()
{
Debug.Log("delayTime is " + delayTime);
resetPosesBuffer(delayTime);
}
void Update()
{
if(tracker == null)
{
return;
}
if (bufferSize > trackerPoses.Count) //grow the array until it reached the buffer size & do nothing until it is full
{
trackerPoses.Add(new trackerPos(tracker.position, tracker.rotation));
return;
}
trackerPoses[counter % bufferSize] = new trackerPos(tracker.position, tracker.rotation);
int followIndex = (counter % bufferSize) + 1; //point to the index furthest from just set
if(followIndex == bufferSize) //handle out of bounds, set back to 0
followIndex = 0;
transform.position = trackerPoses[followIndex].position;
transform.rotation = trackerPoses[followIndex].rotation;
counter++;
}
public void SetTransform(Transform sTracker)
{
tracker = sTracker;
}
public void resetPosesBuffer(float _delayTime)
{
counter = 0;
fps = 1.0f / Time.deltaTime;
Debug.Log("DelayTime is " + _delayTime);
bufferSize = Mathf.CeilToInt(fps * _delayTime);
trackerPoses = new List<trackerPos>();
trackerPoses.Add(new trackerPos(tracker.position, tracker.rotation)); //fill first index
}
}
class trackerPos
{
public Vector3 position;
public Quaternion rotation;
public trackerPos(Vector3 _pos, Quaternion _rot)
{
position = _pos;
rotation = _rot;
}
}
It works really well for the most part but I can not figure out how to code it so that the other followers don’t just end up all stacked on one spot when the leader stops moving. Another issue I have with it is that when I tell a character to stop following and start following again they simply teleport to the leaders current position because there is not positional information for the follower to gracefully move to where the leader is before actually following him. I have read through quite a few threads of people trying to do similar things but none of them really have a good solution to these 2 problems. I think most of them brought up the game Diddy Kong Country as a good example of a 2D platformer with a following system. In it the character that is following seems to readjust itself when it is stacked on top of the player.
I would really appreciate any help or nudges in the right direction.