0
\$\begingroup\$

I'm trying to do some simulation where I move objects procedurally in a loop within one method call. So instead of using Update(), method I do for example:

void Simulate(float deltaTime)
{
   // real logic look a lot different, this is just an example
   for(int i = 0; i < 1000; ++i)
   {
      transform.position += direction * speed * deltaTime;
   }
   .....
}

The reason I'm doing this is because I want consistent 'framerate' by passing that deltaTime parameter; Which is for example 0.01f; And each step of that loop I'm taking snapshots on multiple cameras with calling Render() method on them before taking a picture. The movement works fine and I see it on snapshots, but some objects has an animation which should also be updated procedurally.

I've tried to do

animator.speed = 0; 

and then in the same loop, to test it

for (int i = 1; i < 1000; ++i)
{
   ...
   animator.Play("WalkFWD", 0, i/1000f); //for example
}

But animation does not change over that loop. It only works with that Play method when I either make this Simulation() method a coroutine and pass one frame (yield return null), or if I use in Update(). So, my question: is it possible to procedurally update/redraw animation after normalized time of that animation is set. (without waiting for another frame)

\$\endgroup\$
  • \$\begingroup\$ Does Animator.Update() behave any differently for you than Play()? \$\endgroup\$ – DMGregory Sep 18 '19 at 9:50
  • \$\begingroup\$ @DMGregory, yes. If I do it in a loop within one Unity frame and capture snapshots with camera, then I can see that the animation doesn't change the avatar pose. \$\endgroup\$ – earthQuake Sep 18 '19 at 17:30
  • \$\begingroup\$ @DMGregory, void OnSimulationTick(object sender, SimulationTickArgs args) { currentState.Update(args.deltaTime); anim.Update(args.deltaTime); } I trigger an event, which OnSimulationTick is subscribed to, multiple times within the same unity frame. \$\endgroup\$ – earthQuake Sep 18 '19 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.