How to use infinite loops or alternatively repeated tasks

I know this is all very bad code, but it’s just a concept. I want to have something walk in a square so i’ve given it four points and positions and have it walk there. It should work if i ran it once, but I want it to work forever. How can i make this code work? Alternatively, how can I change it to have my object work between points forever.

Code:

public class movement1 : MonoBehaviour {
public static GameObject target;
public Transform targetPos = target.transform;
public Vector3 targetPosv3 = target.transform.position;
public Transform targetPos2 = target.transform;
public Vector3 targetPosv32 = target.transform.position;
public Transform targetPos3 = target.transform;
public Vector3 targetPosv33 = target.transform.position;
public Transform targetPos4 = target.transform;
public Vector3 targetPosv34 = target.transform.position;
public float step = 100.0f * Time.deltaTime;
// Update is called once per frame
void Update () {
    Start:
    for (int intx = 1; intx < 30; intx++)
    {

        transform.LookAt(targetPos);
        transform.position = Vector3.MoveTowards(transform.position, targetPosv3, step);
        StartCoroutine(MyMethod());
    }
    for (int intx = 1; intx < 30; intx++)
    {

        transform.LookAt(targetPos2);
        transform.position = Vector3.MoveTowards(transform.position, targetPosv32, step);
        StartCoroutine(MyMethod());
    }
    for (int intx = 1; intx < 30; intx++)
    {

        transform.LookAt(targetPos3);
        transform.position = Vector3.MoveTowards(transform.position, targetPosv33, step);
        StartCoroutine(MyMethod());
    }
    for (int intx = 1; intx < 30; intx++)
    {

        transform.LookAt(targetPos3);
        transform.position = Vector3.MoveTowards(transform.position, targetPosv33, step);
        StartCoroutine(MyMethod());
    }
    StartCoroutine(MyMethod());
    goto Start;
}
IEnumerator MyMethod()
{
    yield return new WaitForSeconds(0.01f);
}

}

It doesn’t work in start or in update. What should I do? Thank you very much.

1 Answer

1

Untested, i just moved the code a bit:

 public float speed = 100.0f;
 
void Start()
{
  StartCoroutine( "DoAnimation" );
}

IEnumerator DoAnimation()
{
  while(true)
  {
    for (int intx = 1; intx < 30; intx++)
    {
      transform.LookAt(targetPos);
      transform.position = Vector3.MoveTowards(transform.position, targetPosv3, speed * Time.deltaTime);
      yield return new WaitForSeconds(0.01f);
    }
    
    for (int intx = 1; intx < 30; intx++)
    {
      transform.LookAt(targetPos2);
      transform.position = Vector3.MoveTowards(transform.position, targetPosv32, speed * Time.deltaTime);
      yield return new WaitForSeconds(0.01f);
    }
    
    for (int intx = 1; intx < 30; intx++)
    {
      transform.LookAt(targetPos3);
      transform.position = Vector3.MoveTowards(transform.position, targetPosv33, speed * Time.deltaTime);
      yield return new WaitForSeconds(0.01f);
    }
    
    for (int intx = 1; intx < 30; intx++)
    {
      transform.LookAt(targetPos3);
      transform.position = Vector3.MoveTowards(transform.position, targetPosv33, speed * Time.deltaTime);
      yield return new WaitForSeconds(0.01f);
    }
  }
}

You can’t precalculate “step” using Time.deltaTime. Time.deltaTime is dynamic and can be different for every frame.

I have another script set up to follow the player in a certain radius, and this works but only if I'm in that radius. What should I do? It only faces towards them now but never actually moves. Thank you though, this is a big help. EDIT: I fixed it. I had to set the speed in the editor, it was stuck. Thanks!