Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

What is the best (most performant) way to do a complicated repetitive tasks (like pathfinding)

Discussion in 'Editor & General Support' started by brolol404, Sep 19, 2020.

  1. brolol404

    brolol404

    Joined:
    Feb 14, 2015
    Posts:
    21
    I am trying to optimize my code (without multithreading or including the ECS and Job system), but am not seeing much results from moving things out of the Update function and into Coroutines. I assumed using for (;; ) Coroutines with WaitForSeconds would run less code (as the calculations would happen every second instead of every frame), but if anything the performance seems to be worse. Would using InvokeRepeating functions be a better option? Simply, I am trying to do the following for a thousand units (this code works, but is slow):

    Code (CSharp):
    1. void Start(){
    2. // Locate closest target (WaitForSeconds(1.3f))
    3. StartCoroutine(“FindTarget”);
    4. // Determine damage bonuses if target != null (WaitForSeconds(5.0f))
    5. StartCoroutine(“CalculateDamageModifier”);
    6. // Move to or attack target and deal damage if target != null (WaitForSeconds(0.1f))
    7. StartCoroutine(“ExecuteActions”);
    8. // If lives < 1, die (WaitForSeconds(1.0f))
    9. StartCoroutine(“Death”);}
     
    Last edited: Sep 19, 2020
  2. brolol404

    brolol404

    Joined:
    Feb 14, 2015
    Posts:
    21
    I am still interested in the answer, but adding StopAllCoroutines(); and a yield break in the Death Coroutine did help some as I think the dead unit's coroutines continued to run even after their gameobject was destroyed. I can get about 1,000 units on the screen at 30fps.
     
  3. koirat

    koirat

    Joined:
    Jul 7, 2012
    Posts:
    2,010
    Remove Coroutines form your code and forget about them it's a crap.
    Invoke repeating works in the main thread the same like update and Coroutines.

    If you want performance you need parallelization. For this you want to use Jobs.
     
    mvaz_p likes this.