using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FIMSpace.FLook;
using System.Linq;
public class MultipleLookAtTargets : MonoBehaviour
{
public FLookAnimator lookAnimator;
public List<Transform> targets = new List<Transform>();
public float switchingTime;
public bool switchingLoop = false;
// Start is called before the first frame update
private void Start()
{
lookAnimator.MaximumDistance = 10f;
var targetsTags = GameObject.FindGameObjectsWithTag("Target").ToList();
if (targetsTags != null && targets.Count == 0)
{
for (int i = 0; i < targetsTags.Count; i++)
{
targets.Add(targetsTags[i].transform);
}
}
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.V))
{
StartCoroutine(SwitchTargetsPeriodically());
}
}
IEnumerator SwitchTargetsPeriodically()
{
for (int i = 0; i < targets.Count; i++)
{
lookAnimator.ObjectToFollow = targets[i];
yield return new WaitForSeconds(switchingTime);
}
}
}
How to use the switchingLoop if it’s true to loop over the targets in the coroutine ?
Now it’s iterating over the targets only once but I want that if the flag is true iterate over and over again the targets.
It’s working but if I interrupt the loop in the middle and change the flag switchingLoop to false it will stop at the last target but than if I set the flag switchingLoop back to true again it will not loop nonstop again why is that ?
IEnumerator SwitchTargetsPeriodically()
{
do
{
for (int i = 0; i < targets.Count; i++)
{
lookAnimator.ObjectToFollow = targets[i];
yield return new WaitForSeconds(switchingTime);
}
}
while (switchingLoop);
}
When it’s the flag is true and it’s iterating if in the middle I change the flag to false it will stop but if I change back to true it will not continue the do/while I will need to start the game over again with the flag set to true.