Home > Enterprise >  How to process a method inside Update without waiting for it and without reprocessing it while its w
How to process a method inside Update without waiting for it and without reprocessing it while its w

Time:05-12

I have an AI creature that I want to jump at enemies that fall within it's radius. The radius detects them on every update which is fine but how should I process the Jump?

The jump currently resides inside the Update as well but how should the state be handled where the AI needs to jump and continue other processing while the jump occurs but not try to jump while already jumping.

What is the best way to handle this? Events & Delegates? UnityEvents? Multi Threading? Locking? async? Does the update even allow for some of this, it's called every frame but is it async?

CodePudding user response:

A quick and dirty approach would be to reduce the polling rate of the radius check, so the jump can easily finish before the next radius check occurs.

For the best way to do it, im not sure.

CodePudding user response:

While lots of people are hating on coroutines atm. Ignore that for now.

Have a coroutine that does the detection and jumping that waits after the jump to finish. That way your fps stays good and the calculations won’t effect it.

Pseudo code as im on a phone

IEnumerator findme()
{
  while(true)
  { 
    // find victim
    //if victimfound
      // trigger jump
      // what till animation is not playing any more
   }
}

I would also suggest potentially a wait at the end of the while loop as you almost certainly don’t need it like 200x a second so if not having jumped maybe a short wait like 0.25s

  • Related