Fuey500
Fuey500

Reputation: 72

How to process a method inside Update without waiting for it and without reprocessing it while its working

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?

Upvotes: 0

Views: 35

Answers (2)

BugFinder
BugFinder

Reputation: 17858

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

Upvotes: 1

jluck
jluck

Reputation: 31

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.

Upvotes: 1

Related Questions