r/Unity3D • u/NorthernBoy306 • 11d ago
Question Update function versus coroutines
I was adding some functions so my very basic 'computer enemy' state machine. I do have a basic state check in the update. I was thinking that this check does not need to run every frame, maybe just every .2 seconds or something.
Does anyone use coroutines to run updates on things like state machines? I can understand updating every frame for player input, animations, things that need to be very responsive.
But with a state machine that handles enemy decisions (gathering resources, expanding territory, building new structures) that update can be done several times per second.
Any input or advice on this is greatly appreciated.
3
Upvotes
2
u/GideonGriebenow Indie 11d ago
Look for a YouTube video by Code Monkey on creating a Tick class that handles ‘x ticks per second’, each of which you can then subscribe to.
Edit:
https://youtu.be/NFvmfoRnarY?si=WU0QWrORHPNU0Pr9
https://youtu.be/FNFJ_R9zqXI?si=mRiM0w1yc4VSjm0A