I have it working by timer, but I need to make it work by distance.
I’ve tried making the list an array but either way it tells me out of array range or list range whenever it spawns the first one
I tried posting sample code but then it all got jumbled together. Edit: hopefully the formatting for the code is right
{
private float nextBreadcrumbTime;
private Vector3 movement;
[SerializeField]
private GameObject breadcrumb;
private int breadcrumbCounter;
[SerializeField]
private int maxBreadcrumbs;
[SerializeField]
private float breadcrumbDistance;
private List<GameObject> BreadcrumbsList;
private int index;
private int crumbsLeft;
private Transform lastCrumb;
// Start is called before the first frame update
void Awake()
{
BreadcrumbsList = new List<GameObject>();
}
void Update()
{
crumbsLeft = BreadcrumbsList.Count;
}
void FixedUpdate()
{
index = BreadcrumbsList.IndexOf(breadcrumb);
//if (index >= 0)
//{
// GameObject latestCrumb = objects[index];
//}
destroyLastBreadcrumb();
}
//Vector3 direction
{
{
if (!playerActor.IsSleeping())
{
for (int i = 0; i < maxBreadcrumbs; i++)
{
if (Time.time > nextBreadcrumbTime)
//Vector3 lastCrumbPosition = BreadcrumbsList[index].transform.position;
//if (Vector3.Distance(lastCrumbPosition, transform.position) > 30f)
{
GameObject newBreadcrumb = Instantiate(breadcrumb, new Vector3(transform.position.x, -20, transform.position.z), Quaternion.identity) as GameObject;
//newBreadcrumb.name = "Breadcrumb" + breadcrumbCounter.ToString();
BreadcrumbsList.Add(newBreadcrumb);
//increment next_spawn_time
nextBreadcrumbTime += 5.0f;
}
}
}
}
}
public void destroyLastBreadcrumb ()
{
if (crumbsLeft > maxBreadcrumbs)
{
int lastCrumbIndex = crumbsLeft - (maxBreadcrumbs+1);
GameObject crumbToDestroy = BreadcrumbsList[lastCrumbIndex];
Destroy(crumbToDestroy);
BreadcrumbsList.RemoveAt(lastCrumbIndex);
}
}
//IEnumerator updatePosition()
//{
// latestPosition = playerActor.transform.position;
// yield return new WaitForSeconds(3);
//}
}